数据结构与算法——选择排序

 选择排序

 选择排序的核心是,在每趟比较中,找到本趟中最小的元素放在本趟比较的第1个位置,所以选择排序的每趟比较只需要交换一次即可,只要找到本趟比较中最小的元素和本趟比较中第1位置的元素交换即可。

===========================================================

public static void selectionSort(int[] nums) {

    if (nums == null || nums.length < 2) {
        return;
    }

    for(int i = 0; i < nums.length - 1; i++) {
        int minIndex = i;
        for(int j = i + 1; j < nums.length; j++) {
            if(nums[i] > nums[j]) {
                minIndex = nums[j] < nums[minIndex] ? j : minIndex;
            }
        }
        swap(nums, i, minIndex);
    }

}

===============================================================

just move on
原文地址:https://www.cnblogs.com/ql70me/p/14890557.html