The classic algorithm of quick sort (QuickSort)

insert image description here

Event address: CSDN 21-day Learning Challenge

quick sort

       The elements to be sorted are divided into two independent parts by one-pass sorting, one of which is an element smaller than the reference number, and the other part is an element larger than the reference number. Then the two parts of elements are sorted according to the previous algorithm, until there is only one element in each part.

In essence, quick sort should be regarded as a recursive divide and conquer method based on bubble sort.

Algorithm principle

  • Pick an element from the sequence as a reference point

  • Reorder the sequence, all elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed behind the reference

  • Then on the left and right sides of the reference value, repeat the above steps

  • Sort the arrays on the left and right sides of the reference value element by recursion. After sorting, the entire array is sorted.

diagram

Problem Description:
Given an unordered array nums, it can be output in order

Example:

输入: nums = [431296],
输出: nums = [123469]

The diagram is as follows:

insert image description here

Java code implementation

core code

public class QuickSort {
    
    
    //比较 v 是否小于 w
    public static boolean less(Comparable v,Comparable w){
    
    
        return v.compareTo(w) < 0;
    }
    //数组元素交换位置
    private static void swap(Comparable[] a,int i,int j){
    
    
        Comparable temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    //排序
    public static void sort(Comparable[] a){
    
    
        int l = 0;
        int h = a.length - 1;
        sort(a,l,h);
    }
    private static void sort(Comparable[] a,int l,int h){
    
    
        if (h <= l)  return;
        //对数组进行分组(左右两个数组)
        // i 表示分组之后基准值的索引
        int i = partition(a, l, h);
        //让左边的数组有序
        sort(a,l,i - 1);
        //让有边的数组有序
        sort(a,i + 1,h);
    }
    public static int partition(Comparable[] a,int l,int h){
    
    
        //确定基准值
        Comparable key = a[l];
        //定义两个指针
        int left = l;
        int right = h + 1;
        //切分
        while (true){
    
    
            //从右向左扫描,移动right指针找一个比基准值小的元素,找到就停止
            while (less(key,a[--right])){
    
    
                if (right == l)
                    break;
            }
            //从左向右扫描,移动left指针找一个比基准值大的元素,找到就停止
            while (less(a[++left],key)){
    
    
                if (left == h)
                    break;
            }
            if (left>=right){
    
    
                break;
            }else {
    
    
                swap(a,left,right);
            }
        }
        //交换基准值
        swap(a,l,right);
        return right;
    }
}

public class QuickSortTest {
    
    
    public static void main(String[] args) {
    
    
        Integer[] arr = {
    
    3,1,2,4,9,6};
        QuickSort.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
//排序前:{3,1,2,4,9,6}
//排序后:{1,2,3,4,6,9}

operation result:

insert image description here

Analysis of Algorithms

  • time complexity

       The best case of quick sort is that each fetched element just bisects the entire array. Since quick sort uses recursive calls, the time complexity of quick sort also needs to be calculated using recursive algorithms. T[n] = 2T[n/2] + f(n); the time complexity is O(nlogn). In the worst case, like bubble sort, each comparison needs to exchange elements, and the time complexity is O(n^2).

Therefore, the time complexity of quicksort is: O(nlogn).

  • space complexity

       The space complexity is mainly the use of stack space caused by recursion. In the best case, the depth of the recursive tree is log2n, and the space complexity is O(logn). In the worst case, n-1 recursive calls are required. This The time-space complexity is O(n).

Therefore, the space complexity of quicksort is: O(logn).

Guess you like

Origin blog.csdn.net/weixin_52986315/article/details/126378443