Java programming: sorting algorithm-selection sort

basic introduction

Selective sorting is also an 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.

Selection and sorting ideas:

Select sorting is also a simple sorting method. Its basic idea is: first select the minimum value from arr[0] ~ arr[n-1], exchange with arr[0], and select from arr[1] ~ arr[n-1] for the second time The minimum value, exchange with arr[1], select the minimum value from arr[2] ~ arr[n-1] for the third time, exchange with arr[2],..., the i-th time from arr[i-1] ~ Select the minimum value from arr[n-1], exchange with arr[i-1],..., select the minimum value from arr[n-2] ~ arr[n-1] for the n-1th time, and exchange it with arr[n -2] Exchange, pass n-1 times in total, and get an ordered sequence arranged from small to large according to the sorting code.

Select the sorting idea analysis diagram:

Insert picture description here

Select and sort application examples:

There is a group of cows, the colors are 101, 34, 119, 1 Please use the selection sort to sort from low to high [101, 34, 119, 1]

Insert picture description here

Explanation: The data of the test efficiency is 80,000, depending on the time-consuming
Insert picture description here

Code

package sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class SelectSort {
    
    
    public static void main(String[] args) {
    
    
        //int[] arr = {101, 34, 119, 1,12,43,543};
        /*System.out.println("排序前:");
        System.out.println(Arrays.toString(arr));
        //selectSort1(arr);
        selectSort(arr);
        System.out.println("排序后:");
        System.out.println(Arrays.toString(arr));*/

        // 时间测试
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
    
    
            arr[i] = (int) (Math.random() * 80000);// 生成一个0-80000的数据
        }
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1Str = simpleDateFormat.format(date1);
        System.out.println("排序前的时间为:" + date1Str);
        selectSort(arr);
        Date date2 = new Date();
        String date2Str = simpleDateFormat.format(date2);
        System.out.println("排序后的时间为:" + date2Str);

    }

    public static void selectSort(int[] arr) {
    
    
        // 再推导的过程中,我们发现了规律,因此可以使用循环解决
        // 选择排序的时间复杂度也是O(n²)
        int minIndex = 0, min = 0;
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            minIndex = i;
            min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
    
    
                if (min > arr[j]) {
    
     // 说明假定的最小值并不是最小值
                    min = arr[j];
                    minIndex = j;   // 重置min和minIndex
                }
            }
            // 将最小值放在arr[i]的位置,两个值交换
            if (minIndex != i) {
    
    
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }

    // 选择排序
    public static void selectSort1(int[] arr) {
    
    
        // 使用逐步推导的方式
        // 第一轮
        // 原原始的数组:101, 34, 119, 1
        // 第一轮排序:1, 34, 119, 101
        // 算法:先简单→→→再复杂 把一个复杂的问题拆分为多个简单问题

        // 第1轮
        int minIndex = 0;
        int min = arr[0];
        for (int i = 0 + 1; i < arr.length; i++) {
    
    
            if (min > arr[i]) {
    
     // 说明假定的最小值并不是最小值
                min = arr[i];
                minIndex = i;   // 重置min和minIndex
            }
        }
        // 将最小值放在arr[0]的位置,两个值交换
        if (minIndex != 0) {
    
    
            arr[minIndex] = arr[0];
            arr[0] = min;
        }


        System.out.println("第1轮后:");
        System.out.println(Arrays.toString(arr));

        // 第2轮
        minIndex = 1;
        min = arr[1];
        for (int i = 1 + 1; i < arr.length; i++) {
    
    
            if (min > arr[i]) {
    
     // 说明假定的最小值并不是最小值
                min = arr[i];
                minIndex = i;   // 重置min和minIndex
            }
        }
        // 将最小值放在arr[1]的位置,两个值交换
        // 优化
        if (minIndex != 1) {
    
    
            arr[minIndex] = arr[1];
            arr[1] = min;
        }

        System.out.println("第2轮后:");
        System.out.println(Arrays.toString(arr));

        // 第3轮
        minIndex = 2;
        min = arr[2];
        for (int i = 2 + 1; i < arr.length; i++) {
    
    
            if (min > arr[i]) {
    
     // 说明假定的最小值并不是最小值
                min = arr[i];
                minIndex = i;   // 重置min和minIndex
            }
        }
        // 将最小值放在arr[2]的位置,两个值交换
        if (minIndex != 2) {
    
    
            arr[minIndex] = arr[2];
            arr[2] = min;
        }

        System.out.println("第3轮后:");
        System.out.println(Arrays.toString(arr));
    }
}

in conclusion

80,000 data takes 2-3 seconds, which is faster than bubbling.

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108775797