Interpretation of DualPivotQuicksort source code

Interpretation of DualPivotQuicksort source code Threshold constant Click to view the code /** * The maximum number of r...
General merge sort
Optimized merge sort (TimSort)
Insert sort
Pairwise insertion sort
Single axis quick sort
Two axis quick sort
Count sort
Interpretation of DualPivotQuicksort source code Threshold constant Click to view the code
/** * The maximum number of runs in merge sort. */ private static final int MAX_RUN_COUNT = 67; /** * The maximum length of run in merge sort. */ private static final int MAX_RUN_LENGTH = 33; /** * If the length of an array to be sorted is less than this * constant, Quicksort is used in preference to merge sort. */ private static final int QUICKSORT_THRESHOLD = 286; /** * If the length of an array to be sorted is less than this * constant, insertion sort is used in preference to Quicksort. */ private static final int INSERTION_SORT_THRESHOLD = 47; /** * If the length of a byte array to be sorted is greater than this * constant, counting sort is used in preference to insertion sort. */ private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29; /** * If the length of a short or char array to be sorted is greater * than this constant, counting sort is used in preference to Quicksort. */ private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
sort

General merge sort

It is the idea of "divide and conquer". First disassemble the sequence elements, and then merge them, that is, merge the adjacent ordered subsequences.

Optimized merge sort (TimSort)

Timport's idea is that when "dividing", it is directly divided into various ordered subsequences with different lengths from left to right, and then these subsequences are merged. In this way, the complexity is greatly reduced.

Insert sort

The sequence is divided into two parts, one is orderly and the other is disordered. Elements are continuously selected from the disordered part and inserted into the ordered part. (at first, it was thought that the first element was an ordered part and the other elements were disordered parts)

Pairwise insertion sort

Step 1: take two elements a1 and a2 in the disordered part and adjust them so that a1 > a2;
Step 2: a1 compare to the left, find the appropriate position and insert;
Step 3: a2 just compare on the left side of a1 (a1 > a2) and find the appropriate position to insert.

Single axis quick sort

Step 1: select one of the elements as the axis.
Step 2: both sides start traversing at the same time. The elements larger than the axis are placed on the left and the elements smaller than the axis are placed on the right. (see the following figure for details)
Step 3: recursively process the two sequences separated by the axis, and repeat step 1 and 2. Finally, an ordered sequence is obtained

Two axis quick sort

Step 1: k traverse to the right, great traverse to the left, and put the traversed elements into the appropriate interval.
Step 2: recursively process the three intervals to obtain the ordered sequence.

Count sort

① : first create an array count whose length is the number of elements, and all the elements in it are 0.
② : traverse the sequence to be sorted, find the position of the array count according to the sequence element size a, and count[a]+=1;
(for example, if the element just traversed is 55, count [55] + = 1 is found)
③ : traverse count [] from left to right, and take out all elements that are not 0, according to the number of count[a].
④ : finally get the valid sequence.

Source code Click to view the code