for an input array containing n, the fast sort is an O (n^2) sorting algorithm with time complexity. Although the complexity of the most ring cases is high, fast sorting is often the best choice for the actual application of sorting, because the average performance of the fast-track is very good: its expected complexity is O (NLGN), and the constant factor in O (NLGN) is very small . In addition, fast sorting can also be done in the original order, even in the virtual environment can work well.
1 Description of the Quick sort
As with the merge sort, the quick sort also uses the idea of the divide-and-conquer method, the following is a typical sub-array a[p. R] for quick sorting:
decomposition : Array a[p. R] is divided into two (possibly empty) sub-arrays a[p. Q-1] and a[q+1. R], so that each element in A[p. Q-1] is less than a[q], and each element in A[q+1. R] is greater than a[q]. Q is also part of the partitioning process.
workaround : Sort by recursive call quick Sort, sub-array a[p. q-1] and a[q+1. R]
merging : Because the sub-arrays are sorted by the original, they do not need to be merged, and the array a[p. R] is already sorted.
The following is a quick sort of code:
QUICKSORT (A, p, r) 1 if p<r2 q = PARTITION (A, p, r) 3 QUICKSORT (A, p, q-1) 4 QUICKSORT (A, q+1, R)
In order to sort all the elements of an array A, the initial call Quicksort (A, 1, a.length).
2 Partitioning of arrays
The key part of the algorithm is the partion process, which implements the on-site ordering of the Subarray a[p. R]. The pseudo-code for partion is represented as follows:
Partion (A, P, r) 1 x = a[r]2 i = p-13 for j=p to r-14 if a[u]<=x5 i = i +16 Exchange A[i] W ITH a[j]7 Exchange a[i+1] with a[r]8 return i + 1
Represents the process of how Partion operates on an array containing 8 elements. Pration always chooses a x=a[r] as the principal (pivot element) and surrounds it to divide the array a[p. R].
Prtion the actual complexity on the sub-array a[p. R] is O (n), where n = r-p +1.
3 performance of Quick-arrange
The run time of a fast sort depends on whether the division is balanced, and the balance depends on the element being divided. If the partitioning is balanced, then the fast sorting algorithm performs as well as the merge sort, and if the partitioning is unbalanced, the performance of the fast sort is close to the insertion sort, and the non-formalized fractals of the fast sorting performance are given below:
Worst-Case Division:
The worst case of a fast sort took place when the partitioning produced two sub-problems contained n-1 elements and 0 elements respectively. It may be assumed that this imbalance is present in each recursive invocation of the algorithm. The time complexity of the partitioning operation is O (n). Since the recursive invocation of an array of size 0 is returned directly, therefore, T (0) =o (1), the recursive algorithm run time can be expressed as:
T (n) = t (n-1) + t (0) + O (n) = t (n-1) + O (n)
The solution of recursion can be directly obtained by using the method of the n^2 to T (n) = O (-). Therefore, if the partition is the maximum imbalance at each level of the algorithm, then the time complexity of the algorithm is O (n^2).
Division of the best case
In the most balanced division possible, the size of the two-word problem that partion received was not much larger than N/2. This is because one of the sub-problems is N/2, while the size of the other word problem is N/2-1. In this case, the performance of the quick sort is very good. At this point, the recursion of the algorithm's run time is:
T (n) = t (N/2) + O (n)
It is shown from the main theorem that the solution of the recursive formula is O (NLGN).
Division of the Balance
The average run time for fast sorting is closer to its best case than the worst case scenario. The hypothetical algorithm always produces 9:1 division, at first glance, the division is very unbalanced. The recursion for the time complexity of getting the quick sort is:
T (n) = t (9N/10) + t (N/10) + CN
In the book, the recursive tree is used to find the solution of the above recursion as O (NLGN). It is also pointed out that, as long as the partition is a constant proportion, the algorithm's running time is always O (NLGN).
Introduction to Algorithms Learning Notes Chapter 7th Quick Sort