Quick row definition:
Set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of the array) as the key data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip.
Basic idea:
The idea of quick sorting is the thought of division and treatment.
A quick sort is to find an element (which can theoretically be arbitrarily found) as a datum (pivot), and then partition the array so that the values of the elements on the left of the datum are not larger than the datum, and the element values to the right of the datum are not less than the datum values, so that the elements of the datum are adjusted to the correct position after Recursive quick sorting, and other n-1 elements are also adjusted to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sort is complete. So the core algorithm of the fast sorting algorithm is the partitioning operation, that is, how to adjust the position of the datum and adjust the final position of the return datum to divide and conquer the recursion.
For example, this may not be too good to understand. Suppose that the order to sort is listed as
2 2 4 9 3 6 7 1 5 First, using 2 as the benchmark, use i,j two pointers to scan from both sides, separating the elements smaller than 2 and the elements larger than 2. First compare 2 and 5, 5:2, J shift Left
2 2 4 9 3 6 7 1 5 compare 2 and less than 2, so put 1 in the position of 2
2 1 4 9 3 6 7 1 5 Compare 2 and bis more than 2, so move 4 to the back
2 1 4 9 3 6 7 4 5 Compare 2 and 7,2 and 6,2 and 3,2 and 9, all greater than 2, meet the conditions, and therefore unchanged
After the first round of quick sorting, the elements change to look like this
[1] 2 [4 9 3 6 7 5]
After that, the 2 left side of the element is fast, because there is only one element, so the end of the fast line. The right side of the queue, recursive, and eventually produce the final result.
classprogram{Static voidMain (string[] args) { int[] Array =New[] {234,632, at,643,2,6, -2,423,2342, + }; Console.WriteLine ("before sorting:"); Console.WriteLine (string. Join (",", array)); QuickSort (Array,0, array. Length-1); Console.WriteLine ("after sorting:"); Console.WriteLine (string. Join (",", array)); Console.readkey (); } /// <summary> ///Quick Sort/// </summary> /// <param name= "Sources" >Target Array</param> /// <param name= "left" >Array Minimum index</param> /// <param name= "right" >Array Maximum index</param> Private Static voidQuickSort (int[] Sources,intLeftintRight ) { //terminating recursion, sorting complete if(Left > right)return; intPivot = Sources[left],//Base NumberLow =Left , high=Right ; while(Low <High ) { //descending from right to left while(Low < High && Sources[high] >pivot)--High ; //less than the base number on the leftSources[low] =Sources[high]; //increment judgment from left to right while(Low < High && Sources[low] <pivot)++Low ; //above the base number on the rightSources[high] =Sources[low]; } //once sorted, the left side is less than the base number, and the right is greater than the base numberSources[low] =pivot; //partition recursionQuickSort (sources, left, low-1); QuickSort (Sources, low+1, right); }}
Quick sort of sorting algorithm