Eight sorting algorithms - quick sorting (motion picture understanding)

quick sort

concept

Quick sort is an improvement on bubble sort. The basic principle is to divide the array into two sub-arrays by selecting a reference element, sort the sub-arrays respectively, and finally realize the orderly arrangement of the entire array. The time complexity of quick sort is O(nlogn) at best, and O(n^2) at worst, which is an efficient sorting algorithm.

Algorithm ideas

The basic idea of ​​quick sort is: select a reference element, divide the array into two sub-arrays, after sorting in one pass, the elements smaller than the reference element are on the left, and the elements larger than the reference element are on the right, and then the left and right The sub-arrays are sorted, and finally the orderly arrangement of the entire array is realized. Specifically, the sorting process is as follows:

1. Select a reference element;
2. Divide the array into two sub-arrays, the elements smaller than the reference element are placed on the left, and the elements larger than the reference element are placed on the right; 3.
Sort the left and right sub-arrays separately, and repeat the above operations ;
4. Until the whole array is sorted.

animation

 

algorithm code


    public  static void qSort(int[] a,int left,int right){   //主体
        if(left >= right) return;
        int i = left;
        int j = right;
        int pos = a[left];   //基准数

        while(i < j){
            while(i < j && a[j] >= pos) {
                j--;
            }
            a[i] = a[j];
            while(i < j && a[i] <= pos) {
                i++;
            }
            a[j] = a[i];
        }

        a[i] = pos;  //这步是 left==right了 ,最后把基准值放进去

        qSort(a,left,i-1);  //基准值的左边
        qSort(a,i+1,right);  //基准值的右边

    }

Complexity Analysis

Time complexity best O(nlogn)   worst O(n^2)

Space complexity   best O(logn)    worst O(n)

Unstable   _

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


When testing with 100,000 data:

 It turns out that the stack overflowed. Because in the process of continuous recursion, it is divided into two, and the function is called continuously, just like a binary tree, the more data, the bigger the binary tree. And calling once, you have to open up a space in the stack. At this time, you have to consider using non-recursive implementation.

When testing with 10,000 data: normal

 full code


import java.util.Arrays;
import java.util.Random;

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

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

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

    
    public static void quickSort(int[] a){//包装一下,当我调用的时候就方便了,只需要传一个参数
                                          //int[] a
        qSort(a,0,a.length-1);
    }
    public  static void qSort(int[] a,int left,int right){   //主体
        if(left >= right) return;
        int i = left;
        int j = right;
        int pos = a[left];   //基准数

        while(i < j){
            while(i < j && a[j] >= pos) {
                j--;
            }
            a[i] = a[j];
            while(i < j && a[i] <= pos) {
                i++;
            }
            a[j] = a[i];
        }

        a[i] = pos;  //这步是 left==right了 ,最后把基准值放进去

        qSort(a,left,i-1);  //基准值的左边
        qSort(a,i+1,right);  //基准值的右边

    }


    //生成有序数组  从小到大排列
    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接收
        quickSort(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/132031340