选择排序,快排,冒排

选择排序

//基本思想:选择排序(Selection-sort)是一种简单直观的排序算法。
//它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,
//然后,再从剩余未排序元素中继续寻找最小(大)元素,
//然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕

[7, 6, 5, 3, 1, 9, 4]
------------------------
[1, 6, 5, 3, 7, 9, 4]
[1, 3, 5, 6, 7, 9, 4]
[1, 3, 4, 6, 7, 9, 5]
[1, 3, 4, 5, 7, 9, 6]
[1, 3, 4, 5, 6, 9, 7]
[1, 3, 4, 5, 6, 7, 9]
[1, 3, 4, 5, 6, 7, 9]

    public static void main(String[] args) {

        int[] arr = {7, 6, 5, 3, 1, 9, 4};

        System.out.println(Arrays.toString(arr));
        System.out.println("------------------------");
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i; j < arr.length; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
            }
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;

            System.out.println(Arrays.toString(arr));
        }
    }

https://www.cnblogs.com/snowcan/p/6243873.html

猜你喜欢

转载自www.cnblogs.com/antball/p/11544426.html