The principle of quick sorting is to find a base number for each order, then the element larger than the base number to the right of the base number, the element smaller than the base number to the left of the datum, then the core is to find the position of the base number, the base number where it should be. Now we sort 6 1 2 7 9 3 4 5 10 8.
First find a reference number, which is a reference number, used for comparison. For the sake of simplicity, then choose the first number 6 as the base number well, what we want to do is to find the position of 6, let 6 to the right of the number is larger than it, let 6 the number of left is smaller than the following form.
3 1 2 5 4 6 7 9 10 8
How can we find where 6 is supposed to be? The method is simple, which is to detect from sequence 3 1 2 5 4 6 7 9 10 8 at both ends. Find the right side of the sequence, just find a number 6 higher than the base number to stop, and then from the left of the sequence to find, as long as you find a number 6 smaller than the base number to stop, then swap them two positions. We can then use two variables I and J to point to the leftmost and rightmost of the sequence, respectively. At first, the variable I points to the leftmost part of the sequence, which is the number 6, and J points to the far right of the sequence, pointing to the number 8, as shown in
First J moves from right to left, so long as it finds a number smaller than 6, it stops, and J is in the 5 position. I then start from left to right, as long as I find a number greater than 6 to stop, so it to the position of 7
This is where the two numbers are exchanged.
The first exchange ended, and the sequence became 6, 1, 2, 5, 9, 3 5, 7, 10, 8. The number 5 and the number 7 are exchanged. Now J goes to the left and stops at 4, because 4 is smaller than 6. I then go from left to right, to the 9 position, 9:6 large.
And then swap their positions.
Another exchange is over, our sequence becomes 6 1 2 5 4 3 9 7 10 8. Now that the probe continues, J goes to the left and stops at 3, which is smaller than 6. Then I went to the right, suddenly found I and J met, both to the position of 3, indicating the end of the probe.
The position of the base number 6 should be in the position of 3, we will exchange 3 and 6 position
At this point we finally found the 6 position, which is 6 larger than the number on its right, and the number on the left side of it is less than 6
Fast sequencing of data structures and algorithms-----