Data structure and algorithm notes-------selection sort

Select sort

basic introduction

Selective sorting also belongs to the internal sorting method , which is to select an element from the data to be sorted according to the specified rules, and then exchange positions according to the regulations to achieve the purpose of sorting.

Sorting thought

Select sorting is also a simple sorting method. Its basic idea is :

The first time : Find the smallest value from an array and exchange with array[0], that is to say, instead of finding the smallest one like bubbling, swapping, but traversing the array, finding the smallest one and swapping with array[0], once Only one exchange

Second time : Find the minimum value from array[1]~~~array[n-1] of the array array, and exchange with array[1]. In this process, only one exchange occurs.

Third time : array[2] becomes the minimum value. . . . And so on

Pass a total of n-1 times to get an ordered sequence sorted from small to large according to the sort code

img

Ideas

The first round: Regard the first position as the smallest number, start to loop through the remaining arrays , update the smaller ones to the smallest value, and then find smaller ones than the current one. After one round, find the smallest number and start swapping

The second round: Regard the second position as the smallest number, start to loop through the remaining arrays , compare later and find that it is smaller than him, update the minimum value, and then later, find the smallest number of the remaining numbers. After traversing, Swap with the first position

The third round: the same, starting from the third position

….

Until round n-1

Please listen to the question:

Existing Section ten university students, color values are [100,10,35,24,16,79,4,2,57,80], use the select sort from low to high to sort

Code

The derivation process:

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/21 21:32
 * @Description TODO
 * @pojectname 简单选择排序
 */
public class SelectSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array = {
    
    100,10,35,24,16,79,4,2,57,80};
        selectSort(array);
    }
    //选择排序算法
    public static void selectSort(int[] array){
    
    
        //逐步推到方式
        //第一轮排序
        //原始数组[100,10,35,24,16,79,4,2,57,80]
        //结果:[2,10,35,24,16,79,4,100,57,80]
        int minIndex = 0;//用来存放,最小值的下标
        int min = array[0];//假定现在最小值是数组第一个  也充当了中间值,来交换
        for (int i = 0+1; i < array.length ; i++) {
    
    
            if (min > array[i]){
    
    //说明我们的假定最小值不是最小值
                min = array[i];  //重置最小值
                minIndex = i;   //重置最小值索引
            }
        }
        //循环结束,开始交换
        if (minIndex != 0) {
    
    
            array[minIndex] = array[0];
            array[0] = min;
        }
        System.out.println("第一轮后:"+ Arrays.toString(array));
        //第一轮后:[2, 10, 35, 24, 16, 79, 4, 100, 57, 80]

        //第二轮排序
        minIndex = 1;//用来存放,最小值的下标
        min = array[1];//假定现在最小值是数组第一个  也充当了中间值,来交换
        for (int i = 1+1; i < array.length ; i++) {
    
    
            if (min > array[i]){
    
    //说明我们的假定最小值不是最小值
                min = array[i];  //重置最小值
                minIndex = i;   //重置最小值索引
            }
        }
        //循环结束,开始交换
        if (minIndex != 1) {
    
    
            array[minIndex] = array[1];
            array[1] = min;
        }
        System.out.println("第二轮后:"+ Arrays.toString(array));
        //第二轮后:第二轮后:[2, 4, 35, 24, 16, 79, 10, 100, 57, 80]
        //依次类推
    }
}

In the process of derivation, we found that before each of our for loops, the starting mark is +1 than the starting position of the previous for loop.

Then we can use a for loop to solve

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/21 21:32
 * @Description TODO
 * @pojectname 简单选择排序
 */
public class SelectSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array = {
    
    100,10,35,24,16,79,4,2,57,80};
        System.out.println("排序前"+Arrays.toString(array));
        selectSort(array);
        System.out.println("排序后"+Arrays.toString(array));
    }
    //选择排序算法
    public static void selectSort(int[] array){
    
    
        for (int i = 0; i < array.length -1 ; i++) {
    
    
            int minIndex = i;
            int min = array[i];
            for (int j = i+1; j <array.length ; j++) {
    
    
                if (min > array[j]){
    
    //说明假定的最小值不是最小
                    min = array[j];//重置min
                    minIndex = j;//重置最小值下标
                }
            }
            //交换
            if (minIndex != i){
    
    
                array[minIndex] = array[i];
                array[i] = min;
            }
            System.out.println("第"+i+"轮排序的结果是");
            System.out.println(Arrays.toString(array));
        }
    }
}

Let's deepen the memory with language:

The outer loop controls the first round of sorting, and the inner loop is used to control where our sorting starts

Among them, what are the advantages? We found that our data exchange method is different from our bubble sorting, because this is an exchange, unlike our bubble sorting, which is exchanged every time the requirements are met, so our choice sorting uses the smallest The intermediate auxiliary variable of value index is used to change only once

So some people would think, what if I don’t have to trade in the Nth round? We found that there is an additional layer of if judgment when we exchange . This if judgment is used to judge whether the current minimum subscript is the subscript of the minimum value we assumed at the beginning . If it is not exchanging, it will be output directly. , No exchange occurs, which is a small optimization

Then we measured the time to select sort to sort an array[80000] array, we found that only 2 seconds or 3 seconds , it is indeed much faster than bubble sort

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/111501683