常用排序算法之选择排序

选择排序算法

/**
     * 选择排序
     */
    public static int[] selectSort(int[] array) {
        int position = 0;
        for (int i = 0; i < array.length; i++) {
            int j = i + 1;
            position = i;
            int temp = array[i];
            for (; j < array.length; j++) {
                if (array[j] < temp) {
                    temp = array[j];
                    position = j;
                }
            }

            array[position] = array[i];
            array[i] = temp;
        }

        return array;
    }

运行

/**
     * 待排序的数组
     */
    private static int[] array = {
            12, 10, 5, 9, 5, 32, 16, 1, 9, 99, 80, 3, 18, 19, 20, 25, 7, 15
    };

    public static void main(String[] args) {

        int[] result = SortUtils.selectSort(array.clone());
        System.out.println(Arrays.toString(result));

    }

输出

[1, 3, 5, 5, 7, 9, 9, 10, 12, 15, 16, 18, 19, 20, 25, 32, 80, 99]

你可能感兴趣的:(常用排序算法之选择排序)