Objective:
I looked at it at night before I went to sleep. Halle sitting on the toilet look at the algorithm: fast sorting, a new understanding of the fast sorting algorithm. Before that, I only had some knowledge of the bubbling sort algorithm.
Thank you for Halle's patience.
Known as a one-dimensional array, arr, contains 17 elements:
int[] arr = new int[] { 13, 25, 6, 84, 71, 63, 96, 49, 7, 52, 30, 28, 1, 74, 93, 69, 40 };
We choose any number as a benchmark, such as 25.
Also, declare two pointer variables A and B, placing them at both ends of the array, i.e., 13 and 40.
The left pointer A is responsible for finding a number larger than the base value, which moves from left to right;
The right pointer B is responsible for finding a number smaller than the base value, which moves from right to left.
When one of them finds the appropriate number, it stays here and waits for the other pointer to find the appropriate number. If the other pointer also finds the appropriate number, then the number that they are exchanging (pointer b starts moving).
First-time stay results:
84,1
OK, swap the two numbers for the position. This becomes the following:
(1) 13, 25, 6, 1, 71, 63, 96, 49, 7, 52, 30, 28, 84, 74, 93, 69, 40
A b
At this point, the value of the pointer a dwell position is 1, and the value of the position of Pointer B is 84.
Second stop result:
71,7
Swap the two numbers again in the position:
(2) 13, 25, 6, 1, 7, 63, 96, 49, 71, 52, 30, 28, 84, 74, 93, 69, 40
A b
At this point, the value of the pointer a dwell position is 7, and the value of the position of Pointer B is 71.
At this point we will find that the number between 7 and 71 is greater than 25. This means that if pointer B continues to search to the left for a value smaller than 25, it will definitely meet pointer a. That is, both pointer A and B are in the 7 position.
OK, Exchange The 7 of the pointer A and B together with the base number 25:
(3) 13, 7, 6, 1, 25, 63, 96, 49, 71, 52, 30, 28, 84, 74, 93, 69, 40
Ab
At this point we will find that each number in the left column of the base number 25 is smaller than 25, and each number in the right-hand column is greater than 25. In this way, we can divide the array into two columns, which are bounded by the base value 25:
13, 7, 6, 1
63, 96, 49, 71, 52, 30, 28, 84, 74, 93, 69, 40
In both of these columns, the left hand of the first sequence and the value of the right hand rest of the second column are the same as the left and right pointers to the array.
Here, some people may have doubts: if after step (2), the first move is pointer a instead of pointer B, then whether it is possible?
I can say to you very clearly--absolutely!
As for why, we continue to analyze:
If we first move the pointer a rather than pointer B, then when pointer A is first moved and stays at a value greater than 63 of the base number, pointer B will meet with pointer a when it looks to the left for a value that is less than the base number 25. That
(4) 13, 25, 6, 1, 7, 63, 96, 49, 71, 52, 30, 28, 84, 74, 93, 69, 40
Ab
We will observe that all the numbers on the left side of 63 are not greater than 25, and all numbers 63 and right are not less than 25.
Similarly, we can divide the array (4) into two series:
13, 25, 6, 1, 7
63, 96, 49, 71, 52, 30, 28, 84, 74, 93, 69, 40
In the series (3), we find that the conclusion of (4) is also used in (3):
13, 7, 6, 1, 25, 63, 96, 49, 71, 52, 30, 28, 84, 74, 93, 69, 40
Ab
The conclusion is similar to (4): All numbers on the left side of 25 are not greater than 25, and all numbers 25 and right are not less than 25.
Only in (3), we conclude that all the numbers on the left side of 25 are less than 25, and all the numbers on the right are greater than 25.
Based on the derivation process above, it is easy to sort the two types of sub-series according to the new base number.
In fact, the most important difference between a quick sort and a bubbling sort is that the latter's sort is progressive (two numbers per comparison are contiguous), while the former sort is jumping. When the pre-ordered array is long enough and the number of comparisons is certain, the leap that involves more new values undoubtedly has an incomparable advantage. Of course, the former is equal to the latter in the worst case comparison.
However, don't forget: The benchmark for fast sorting must be a number between the maximum and minimum values of the current sorted sequence. Otherwise, stackoverflowexception can easily occur.
Fundamentals of Fast Sorting