Quick sorting, Time complexity O (N*LOGN), to be able to master!
The following main reference http://blog.csdn.net/morewindows/article/details/6684558, thank Yumbo Lord!
The basic idea of this method is:
1. First, a number is taken from the series as the base number.
2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.
3. Repeat the second step for the left and right intervals until there is only one number for each interval.
Although the quick sort is called the divide-and-conquer method, the three words of the divide-and-conquer method clearly fail to generalize all the steps of the quick sort. So my quick sort is further explained: Pit filling + divide-and-conquer method:
Let's take a look at the example, and give it the definition below (it would be better to summarize the definition in your own words, which would be helpful for implementing the code).
Taking an array as an example, the first number of intervals is the base count.
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
72 |
6 |
57 |
88 |
60 |
42 |
83 |
73 |
48 |
85 |
Initially, i = 0; j = 9; X = A[i] = 72
Since the number in a[0] has been saved to X, it can be understood that a hole has been dug in the array a[0], and other data can be populated here.
Looking forward from J to a number smaller than x or equal to X. When j=8, meet the conditions, will a[8] dug up and then fill in the last pit a[0]. A[0]=A[8]; i++; Such a pit a[0] was done, but formed a new pit a[8], what to do? Simple, and then find the number to fill a[8] this pit. This time from I began to find a number greater than X, when i=3, meet the conditions, will a[3] dug up and then filled in the last pit a[8]=a[3]; j--;
The array becomes:
57
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
48 |
6 | TD valign= "Top" >
88 |
60 |
42 |
83 |
73 |
88 |
85 |
i = 3; j = 7; x=72
Repeat the above steps, looking forward from behind, then looking backwards .
Start looking forward from J, when j=5, meet the conditions, will a[5] dug into the last pit, a[3] = a[5]; i++;
From I start looking backwards, when i=5, due to i==j exit.
At this point, I = j = 5, and a[5] is just the last hole dug, so X is filled into a[5].
The array becomes:
57
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
48 |
6 | TD valign= "Top" >
42 |
60 |
72 |
83 |
73 |
88 |
85 |
You can see that the number before a[5] is less than it, and the number behind A[5] is greater than it . So again to a[0 ... 4] and a[6 ... 9] These two sub-zones repeat the above steps.
And finally the code implementation:
1#include <iostream>2 using namespacestd;3 4 voidQuickSort (intM[],intLintr) {5 inti = l, j = r, Basenumber =M[l];6 7 if(L <r) {8 while(I <j) {9 // First RightTen while(I < J && M[j] >basenumber) Onej--; A - if(I <j) -m[i++] =M[j]; the - //Second Left - while(I < J && M[i] <=basenumber) -i++; + - if(I <j) +m[j--] =M[i]; A } at -M[i] =Basenumber; -QuickSort (M, L, I-1); -QuickSort (m, i +1, R); - } - } in - intMain () { to intm[Ten] = {5,6,3,2,4,7,8,1,0,9}; +QuickSort (M,0,Ten); - for(inti =0; I <Ten; i++) thecout << M[i] <<" "; * $ return 0;Panax Notoginseng}
"Algorithm" QuickSort