简单选择排序

冒泡的基本思想是不断的比较,而选择排序的简单思路是在每一趟中记录最小或者最大,依次往下选择,实现代码如下:

public class SelectSort {

    public static void selectSort(int[] a) {

        int min;

        for (int i = 0; i < a.length; i++) {
            min = i;
            for (int j = i + 1; j < a.length - 1; j++) {
                if (a[j] < a[min]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }

        }
    }

    public static void main(String[] args) {

        int[] test = { 2, 5, 12, 2, 3, 67, 48, 9, 23 };

        selectSort(test);

        for (int i = 0; i < test.length; i++) {
            System.out.println(test[i]);
        }

    }

}

  

2
2
3
5
9
12
48
67
23

原文地址:https://www.cnblogs.com/qgzhan/p/3227241.html