- Fast sequencing (using a split strategy)
- First, a number is taken from the series as the base number.
- The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.
- Repeat the second step for the left and right intervals until there is only one number for each interval.
- Actual combat
There is such an array:
int arr[]={ 5,7,4,6,3,9,2 }
The quick sort is this:
is to0Value of the number bit5As well as the base number. By the way, virtual a temporary variable key to hold this base numberintKey =arr[0]。 A virtual variable is used to hold the next start position from the left to the right.int Left=0A virtual variable is used to hold the next starting position from the right to the left.int Right=6First, from the right.int Right=6Start looking for the first one than5A small number, found6Value of the number bit2Than5Small, and then will6The value of the number bit is assigned to the0Number, this timeint Right=5,int Left=0; Arr is this way {2,7,4,6,3,9,2}, because6Value of the number bit2has been assigned to1Number of digits, at this time6A value is required for the number bit. That's from the ARR array to the leftint Left=0Start looking for the first match value greater than5, found1Value of the number bit7Greater than5, this will1Number of digits7Assign a value to6Number, this timeint Right=5,int Left=2Arr is like this: {2,7,4,6,3,9,7}, and so on, from the rightint Right=5Find the first one less than5Value, found the4Value of the number bit3Meet the criteria, after the assignment is complete, this timeint Right=3,int Left=2Arr is like this: {2,3,4,6,3,9,7}, again from the left to find the first one greater than5The value that is found3Value of the number bit6Meet the criteria, after the assignment is complete, this timeint Right=3,int Left=4Arr is like this: {2,3,4,6,6,9,7}, because Left> Right, the description continues to find, the scope of the lookup has crossed, so it is not necessary to assign the value of the key to3Arr is this way {2,3,4,5,6,9,7At this point the basic has been divided to5The benchmark can be seen as two parts, namely {2,3,4},{6,9,7}, again using the method just now < so called recursion > can finally get the answer smoothly.
- Code
First, virtual variables to do things:intKey = arr[0]int Left=0;int Right= Arr.length; Under what circumstances you do not need to look again:if( Left< Right{} to find the first qualifying number from right to left, and assign a value while(arr[ Right]>key&& Left< Right){ Right--; }if( Left< Right) {arr[ Left++]=arr[ Right]; } Find the first qualifying number from left to right, and assign a value while(arr[ Left]<key&& Left< Right){ Left--; }if( Left< Right) {arr[ Right++]=arr[ Left]} finally does not meet the criteria Left< Right, the value will be assigned while( Left< Right) {arr[ Left]=key; The first time you've got it right, it's down to calling yourself again.
Gotta write a way.
Write a method because you want to call yourself;Private Static void Quick_sort(intS[],intLintR) {if(L < R) {inti = l, j = r, x = S[l]; while(I < J) { while(I < J && S[j] >= x) j--;if(I < j) s[i++] = S[j]; while(I < J && S[i] < x) i++;if(I < j) s[j--] = s[i]; } S[i] = x; Quick_sort (S, l, I-1);//Recursive invocationQuick_sort (s, i +1, R);//Recursive invocation} }
Quick sort of Java