Select the elements to do the pivot element
The usual, not well-thought-out option is to use the first or last element as a pivot element. Select the first element as the pivot element of the program example can refer to the topic of the previous article "Learning in the fast sequence", and select the last element to use as a hub element of the program example can refer to the "Fast sequencing of learning: Fast sequencing process," an example of the introduction of the algorithm.
The sorting process for selecting the last element as a pivot element is this:
If the input is random, then this is acceptable, but if the input is pre-ordered or reversed, then such a pivot element will produce a poor segmentation, because all the elements are not drawn into the S1 or are drawn into the S2. Furthermore, this happens in all recursive calls.
In fact, if the first element is used as a pivot element and the input is pre-ordered, then the quick sort takes two times. However, the pre-ordered input (or the input of a large number of pre-sorted data) is also quite common, so the algorithm using the first element as the hub element is not efficient.
Another idea is to select the larger of the first two different keys as the pivot element, but this is almost as efficient as selecting the first element as the pivot element.
Randomly select pivot element
A safe approach is to randomly select the pivot element.
In general, this strategy is very safe, unless there is a problem with the random number generator, because a random pivot element cannot always produce inferior segmentation in succession. On the other hand, the generation of random numbers is generally expensive and does not reduce the average elapsed time of the rest of the algorithm at all.
The method of dividing in three numbers
The median of a set of N numbers is the largest number of [N/2]. The best choice for the pivot element is the median of the array.
However, this is difficult to figure out, and will significantly slow down the speed of sorting. The median estimate can be obtained by randomly selecting three elements and using their median value as the pivot element. In fact, randomness does not help much, so the general practice is to use the median value of the three elements on the left, right, and center positions as pivot elements. It is obvious that the three-digit median segmentation method eliminates the bad case of pre-sorted input.
This kind of pivot element chooses the fast platoon, the efficiency is quite high.
The quick sort also has a lot of variants, and we're only looking at a few representative algorithms here.
April 28 Morning Post: The reduction of the volume of consolidation "Octopus investment banking": http://licai.daiyuline.com/gupiao/315.html
Silver price hit a new high in recent years Guangdong Rong Chona gold and silver spot overturned investment industry: http://licai.daiyuline.com/guijinshu/314.html
Learning in fast sequencing: Hub meta-selection and algorithmic efficiency