DataStructure Sort 2
Quick Sort
Divide and Conquer
Choosing the Principal Component
Remark: rand() is not very efficient!
One common approach is to use the median number at the head, tail and center of the array:
1 | ElementType Median3(ElementType A[], int Left, int Right) { |
Separating Subsets
The best part of quick sort is that once the separating part is finished, the principal component is placed in the correct place!
When dealing with a relative small range of numbers, the quick sort method using recursion is not very fast. The solution is also very simple: using a hybrid method. (Setting a threshold called Cutoff)
The Code
1 | void QuickSort(ElementType A[], int Left, int Right) { |
Table Sort
When we are not sorting simply numbers but some very large structs like a book, if we want to move this element, we cannot underestimate the cost. In this case, we need table sort.
Indirect Sorting
| A | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] |
|---|---|---|---|---|---|---|---|---|
| key | f | d | e | a | g | b | h | e |
| table | 3 | 5 | 2 | 1 | 7 | 0 | 4 | 6 |
Physical Sorting
Theorem: The sequence of N numbers can be decomposed to several independent rings.
- The best case: all things are sorted
- The worst cast:
- N/2 rings, each has 2 elements
- 3N/2 moves
Cardinal Sorting
Bucket Sort
Cardinal Sort
Least Significant Digit
Sorting with Many Keywords
Most Significant Digit



