Quick sort is an improvement on bubble sort. Divide the data to be sorted into two independent parts through one-pass sorting. All the data in one part is smaller than all the data in the other part. Then use this method to quickly sort the two parts of the data respectively. The entire sorting process can Proceed recursively, and finally the entire data becomes an ordered sequence.
Assume that the array to be sorted is A[0]...A[N-1]. First, randomly select a data (usually the first number of the array) as the benchmark data, and then all numbers smaller than it are Put it in front of it, and all numbers larger than it are put behind it. This process is called one-pass quick sort. It is worth noting that quick sort is not a stable sorting algorithm, that is, the relative positions of multiple identical values may change at the end of the algorithm.
The algorithm of one-pass quick sort is:
1) Set two variables low and high. When sorting starts: low=0, high=N-1;
2) Use the first array element as the reference data and assign it to base, that is, base=A[0];
3) Search forward from high, that is, search forward from back (high--), find the first value A[high] that is less than base, and exchange A[high] and A[low];
4) Search backward from low, that is, search from front to back (low), find the first A[low] that is greater than base, and exchange A[low] and A[high];
5) Repeat steps 3 and 4 until low=high;
Efficiency:
Time complexity: Best: O(nlog2n), Worst: O(n^2), Average: O(nlog2n).
Space complexity: O(nlog2n).
Stability: Unstable.