Understanding and related c ++ code to quickly sort

Quick sort: a set of data, the numbers can be left as a pivot (on the right can be), the next thing to do is, find the right start number is smaller than the pivot,

To find the number is larger than the pivot from the left, and then these two numbers are exchanged, repeat the above steps to find out the number of all eligible exchange,

Finally, larger than the pivot into the pivot between the number and the number is smaller than the pivot. The reason why the right from the start looking and find a number smaller than the pivot is because of the small number of the exchange on the left of the pivot.

Here's an example of special hope to increase understanding.

 

1

9

8

5

6

7

3

2

0

4

1 from right to left to find smaller than the first number is 0, then the index position j = 8, and then from left to right to find than a larger first number is 9, then the position of the index i = 1, an exchange was 0 and 9,

1

0

8

5

6

7

3

2

9

4

Repeat once proceed to the next task

1 from right to left to find smaller than the first number is 0, then the position of the index, j = 1, from left to right to find the first number is larger than the index at this time is 8 1 i = 2, obviously i> j, which is not allowed,

So this time can be selected so that the value of j on the pivot position of the exchange 1 (i.e. the intermediate pivot into the two sets of numbers)

0

1

8

5

6

7

3

2

9

4

Look at the situation this time on a 1 to the left of the number 1 already lined up,

Look at the situation on the right, starting from the position to the last j + 1, j + 1 and the position of the pivot,

8 looking from the right smaller than the first number is 4, the index j = 9, 8 find larger than the first number from the left to 9, the index i = 8, 9 and the exchange 4

8

5

6

7

3

2

4

9

According to the above logic continues

9

5

6

7

3

2

8

9

 

8 left

 

4

5

6

7

3

2

From right to left looking smaller than the number 4, from left to right to find 4 large specific numbers, and exchange

4

2

6

7

3

5

carry on

4

2

3

7

6

5

carry on

3

2

4

7

6

5

8 is a left turn divided into two segments 4

The right 8

9

 

 

Left 4

3

2

 

2

3

 

4 of the right

7

6

5

This time, the same as a pivot to the left, from the right found 5, the left would have been looking for to know where to find the 5 position at this time j = i

7 out of the loop directly to the exchange position and j, so that the hub 7 separating these three numbers, in fact, the right 7 is not worth it

Just consider the left 7

5

6

7

 

So eventually lined up

0

1

2

3

4

5

6

7

8

9

The following is a c ++ with templates fast sorting code

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 template<class T>
 6 void QuickSort(T *q, int left, int right);
 7 
 8 int main()
 9 {
10     int a[]={1,9,8,5,6,7,3,2,0,4};
11     QuickSort(a, 0, 9);
12     for(int i=0; i<10; i++)
13         cout << a[i] << endl;
14     return 0;
15 }
16 
17 template<class T>
18 void QuickSort(T *q, const int left, const int right)
19 {
20     if(left<right)
21     {
22         int i=left, j=right;
23         int temp = q[left];
24         while(i<j)
25         {
26             while(q[j]>=temp && i<j)
27             {
28                 j--;
29             }
30             while(q[i]<=temp && i<j)
31             {
32                 i++;
33             }
34             swap(q[i],q[j]);
35         }
36         swap(q[left], q[j]);
37         QuickSort(q, left, j-1);
38         QuickSort(q, j+1, right);
39     }
40 }

 

Guess you like

Origin www.cnblogs.com/yang901112/p/11332763.html