Eight sorting algorithms - selection sorting (motion picture understanding)

selection sort

Algorithm ideas

Each time select the smallest (or largest) element from the data elements to be sorted and store it at the beginning of the sequence until all the data elements to be sorted are exhausted.

 Steps to select sort:

1> First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence.
2> Continue to find the smallest (largest) element from the remaining unsorted elements, and then put it at the starting position of the unsorted sequence.
3> Repeat the second step until all elements are sorted.

animation

 

algorithm code

public static void selectSort1(int[] a){

        for (int i = 0; i < a.length; i++) {
            int minIndex = i;
            for (int j = i+1 ; j <a.length ; j++) {
                if(a[j] <= a[minIndex]){
                    minIndex = j;
                }
            }
            if(minIndex == i) continue;  //说明没找到更小的
            int tmp = a[minIndex];
            a[minIndex] = a[i];
            a[i] = tmp;
        }
    }

Complexity Analysis

Time complexity O(n^2) Arithmetic sequence

Space complexity O(1)

Stability Unstable

selection sort optimization

 The optimization idea of ​​selection sorting is generally to find the maximum and minimum values ​​at the same time in one traversal, and put them at both ends of the array, so that the number of traversals can be reduced by half. The first time to select the maximum value and the minimum value, the process is as follows:

algorithm code 

public static void selectSort2(int[] a){

        int left  = 0;
        int right = a.length-1;
        while(left<right) {
            int minIndex = left;
            int maxIndex = right;
            for(int i = left+1;i<=right;i++) {
                if(a[i] < a[minIndex]) {
                    minIndex = i;
                }
                if(a[i] > maxIndex) {
                    maxIndex = i;
                }
            }

            //交换左边
            int tmp1 = a[minIndex];
            a[minIndex] = a[left];
            a[left] = tmp1;

            if(maxIndex == left) {   //很重要的一点细节
                maxIndex = minIndex;
            }

            //交换右边
            int tmp2 = a[maxIndex];
            a[maxIndex] = a[right];
            a[right] = tmp2;

            left++;
            right--;

        }
    }

time complexity test


Next, let's try to test it with a lot of data.

int[] a = new int[10_0000]; //100,000 data test

1. The orderArray function is implemented to generate a basic ordered sequence, that is, arranged from small to large.

public static void orderArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
 }

2. The notOrderArray function generates a reverse sequence, that is, arranged from largest to smallest.

public static void notOrderArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = a.length-i;
        } 
}

3. The randomArray function generates a random unordered array.

 public static void randomArray(int[] a) {
        Random random = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(10_0000);
        }
 }

4. The testInsertSort function tests the return value of System.currentTimeMillis() in milliseconds.

 public static void testInsertSort(int[] a){
        int[] tmpArray = Arrays.copyOf(a,a.length);
        long startTime = System.currentTimeMillis();    //注意用long接收
        shellSort(tmpArray);
        long endTime = System.currentTimeMillis();  //返回单位是毫秒
        System.out.println("选择排序耗时:"+(endTime-startTime));
  }

5. Main function call execution

public static void main(String[] args) {
 
        int[] a = new int[10_0000];
        //有序
        System.out.println("基本有序数列");
        orderArray(a);
        testInsertSort(a);
 
        //倒序
        System.out.println("逆序数列");
        notOrderArray(a);
        testInsertSort(a);
 
        //随机乱序
        System.out.println("无序数列");
        randomArray(a);
        testInsertSort(a);
}

Test Results

 From the results, for a large amount of data, the optimized one is slower. It should be that this sorting algorithm is more suitable for a small amount of data.

 

We put 250 data for testing.

                           The results show that the time consumption is still reduced.                                   

full code

import java.util.Random;


public class sort {
    public static void main(String[] args) {
    
        int[] a = new int[10_0000];
        //有序
        System.out.println("基本有序数列");
        orderArray(a);
        testInsertSort(a);

        //无序
        System.out.println("逆序数列");
        notOrderArray(a);
        testInsertSort(a);

        //乱序
        System.out.println("无序数列");
        randomArray(a);
        testInsertSort(a);

    }

    public static void selectSort(int[] a){

        for (int i = 0; i < a.length; i++) {
            int minIndex = i;
            for (int j = i+1 ; j <a.length ; j++) {
                if(a[j] <= a[minIndex]){
                    minIndex = j;
                }
            }
            if(minIndex == i) continue;  //说明没找到更小的
            int tmp = a[minIndex];
            a[minIndex] = a[i];
            a[i] = tmp;
        }
   }


    //生成有序数组  从小到大排列
    public static void orderArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
    }

    //n无序 其实就是从大到小排列
    public static void notOrderArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = a.length-i;
        }
    }

    //乱序 随机生成序列
    public static void randomArray(int[] a) {
        Random random = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(10_0000);
        }
    }

    //大量数据测试
    public static void testInsertSort(int[] a){
        int[] tmpArray = Arrays.copyOf(a,a.length);
        long startTime = System.currentTimeMillis();    //注意用long接收
        selectSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序耗时:"+(endTime-startTime));
    }
}

It is not easy to create, if this blog is helpful to you, please remember to leave a message + like it.

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/132028903