I. Basic Ideas
The list is divided into two sublists by using a reference value. The specific process is as follows:
Place the reference value in the correct position, put the element smaller than the reference value in a sublist, and put the element greater than the reference value in the other sublist.
This is the idea of quick sort.
The fast sorting algorithm provides the fastest sorting technology currently known. In addition to some extremely special cases, the fast sorting algorithm is applicable to almost all occasions.
Ii. Algorithm Description
Quick sorting uses a series of recursive calls to divide the list into smaller and smaller sublists, which are located around the benchmark value. Each recursive step is selected based on the reference that serves as the center point value of the list. The segmentation algorithm completes the exchange so that the reference value is placed in the correct position in the list after the elements are finally sorted.
In this case, the sublist contains only the elements smaller than or equal to the benchmark value, and the sublist contains only the elements greater than or equal to the benchmark value. The original array is converted into an ordered array after recursive descent. Note that elements are placed in a temporary array when elements are sorted by merging and sorting. Fast sorting refers to the re-sorting of sequences by exchanging elements in the sublist. Therefore, fast sorting is a "in-place (in-place) algorithm ".
Iii. Sample Code
public class Quick{ public static void QuickSort(int[] arr) { // call QSort QSort(arr, 0, arr.Length); } private static void QSort(int[] arr, int first, int last) { // index of the pivot int pivotLoc; // temp used for an exchange in a 2-elements list, int temp; // if the range is not at least two elements, return if (last - first <= 1) { return; } // if sublist has two elements, compares arr[first] and // arr[last-1] and exchange if necessary else if (last - first == 2) { if (arr[last - 1] < arr[first]) { temp = arr[last - 1]; arr[last - 1] = arr[first]; arr[first] = temp; } return; } else { pivotLoc = PivotIndex(arr, first, last); // make the recursive call QSort(arr, first, pivotLoc); // make the recursive call QSort(arr, pivotLoc + 1, last); } } private static int PivotIndex(int[] arr, int first, int last) { // index for the midpoint of [first,last] and // the indices that scan the index range in tandem int mid, scanUp, scanDown; // pivot value and array used for exchange int pivot, temp; // empty list if (first == last) { return last; } else if (first == last - 1) { // 1-elements sublist return first; } else { mid = (first + last) / 2; pivot = arr[mid]; // exchange the pivot and the low end of the range // and initialize the indices scanUp and scanDown scanUp = first + 1; scanDown = last - 1; // manage the indices to locate elements that are in // the wrong sublists; stop when scanDwon <= scanUp for (; ; ) { // move up the lower sublist; continue so long as // scanUp is less than or equal to scanDown and // the array value is less than pivot while (scanUp <= scanDown && arr[scanUp] < pivot) { scanUp++; } // move down the upper sublist so long as the array // value is greater than the pivot while (arr[scanDown] > pivot) { scanDown--; } // if indices are not in their sublists, partition complete if (scanUp >= scanDown) { break; } // indices are still in their sublists and identity // two elements in wrong sublists;exchanges temp = arr[scanUp]; arr[scanUp] = arr[scanDown]; arr[scanDown] = temp; scanUp++; scanDown--; } // copy pivot to index (scanDwon) that partitions // sublists and return scanDwon arr[first] = arr[scanDown]; arr[scanDown] = pivot; return scanDown; } }}
Iv. Efficiency Analysis
Unstable.
Space complexity: O (1)
Time Complexity: O (nlog2n)
Worst case: O (n2). to sort the array in basic order, take the maximum (minimum) element of the reference value and degrade it to bubble.
Best case: the number of elements on both sides of the O (nlog2n) benchmark value is basically the same.