选择排序
Code
package kb.algorithm;
public class SelectionSort {
    public static void main(String[] args) {
        int[] a = new int[]{3, 6, 4, 7, 2};
        sort(a);
        StringBuilder sb = new StringBuilder(20);
        for (int i = 0; i < a.length; i++) {
            sb.append(a[i]);
            sb.append(",");
        }
        System.out.println(sb);
    }
    public static void sort(int[] a) {
        int minIndex = 0;
        int temp = 0;
        int len = a.length;
        for (int i = 0; i < len - 1; i++) {
            minIndex = i;
            for (int j = i + 1; j < len; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }
            temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }
}
执行结果
2,3,4,6,7,
分析演示

每次找出一个最小的放到前面。
- 最好、平均,最坏O(n^2)。
- 非稳定排序。
作者:iBrake
        
        本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  
     
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号