Java排序之直接选择排序
生活随笔
收集整理的這篇文章主要介紹了
Java排序之直接选择排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、基本概念
首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再從剩余未排序元素中繼續尋找最小(大)元素,然后放到已排序序列的末尾。以此類推,直到所有元素均排序完畢。
?
?
二、算法特點
是否穩定: false
平均時間復雜度:O(n^2)
最差時間復雜度:O(n^2)
最優時間復雜度:O(n^2)
?
?
三、相關代碼
public static void main(String[] args) {int[] a = { 2, 5, 5, 3, 9, 6, 1, 4, 8, 7 };select_sort(a);print_array(a);}public static void select_sort(int[] a) {for (int i = 0; i < a.length; i++) {for (int j = i + 1; j < a.length; j++) {if (a[i] > a[j]) {swap(a, i, j);}}} }public static void swap(int[] a, int b, int c) {if (b == c)return;a[b] = a[b] ^ a[c];a[c] = a[b] ^ a[c];a[b] = a[b] ^ a[c];}public static void print_array(int[] a) {if (a.length == 0)return;for (int i : a) {System.out.print(i + " ");} }運行結果:
1 2 3 4 5 5 6 7 8 9轉載于:https://www.cnblogs.com/e241138/archive/2012/10/17/2728633.html
總結
以上是生活随笔為你收集整理的Java排序之直接选择排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]关掉myeclipse下的chec
- 下一篇: Ubuntu 12.04 Server