Sort Object
- The sort object needs to implement the comparable interface, and the comparable contains the CompareTo () method
- The built-in comparable objects are: Integer,double,string,date,file ...
- CompareTo () method, less returns -1,equal returns 0,more returns +1
Select sort (Selection sort)
- Cursor moves from left to right
- Retrieves the minimum value of all elements to the right of the cursor before each move, swapping it with the cursor position element
public static void sort(Comparable[] a){ int N = a.length; for (int i = 0; i < N; i++){ int min = i; for (int j = i+1; j < N; j++) if (less(a[j], a[min])) min = j; exchange(a, i, min); }}
Time: \ (n^2/2\) compare and N displacement operation, for any case, run slower
Insert sort (Insertion sort)
- Compares the element of the cursor i position to the element on its left, and if the cursor element is small, the displacement
public static void sort(Comparable[] a){ int N = a.length; for (int i = 0; i < N; i++) for (int j = i; j > 0; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); else break;}
Unavailable:
- For random arrays, the compare of \ (n^2/4\) and \ (n^2/4\) are exchanged
- For an array of ascending (ascending), N-1 times compare,0 times Exchange
- For an array of descending (descending), the interchange of \ (n^2/2\) compare with \ (n^2/2\)
- For partially sorted arrays, Time is CN
Hill Sort (Shellsort)
Change the array a[n] to a matrix b[w][], that is, press W to A[n] elements from left to right, from top to bottom in Matrix B, and then sort each column in B, usually with an insert sort.
For W will take an increment sequence, for example {1,4,13,40,121,364 ... 3W[I-1]+1}, first select the largest w less than N, then sort, then select the new w for calculation until W=1
3x+1 sequence time is \ (O (N^{3/2}) \)
Merge sort (mergesort)
Using the idea of divide and conquer , the basic ideas are as follows:
- Divide the sequence into two parts
- Iterating over each part of the sort
- Merge two parts
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi){ if (hi <= lo) return; int mid = lo + (hi - lo) / 2; sort(a, aux, lo, mid); sort(a, aux, mid+1, hi); merge(a, aux, lo, mid, hi);}
Spents: \ (O (N*LGN) \)
Actual usage changes:
- For smaller-length sequences (such as 7 or less), insert sort
- If two sections are already differentiated by size A[mid] < a[mid+1], then connect directly, not merge
Quick Sort (quicksort)
- Reset Array (Guaranteed efficiency)
- Group: Select A[j], place the element less than A[J] on its left, and the element greater than a[j] to the right
- Recursive grouping of left and right parts
public class Quick{ private static int partition(Comparable[] a, int lo, int hi){ int i = lo, j = hi+1; while (true) { while (less(a[++i], a[lo])) if (i == hi) break; while (less(a[lo], a[--j])) if (j == lo) break; if (i >= j) break; exch(a, i, j); } exch(a, lo, j); return j; } public static void sort(Comparable[] a) { StdRandom.shuffle(a); sort(a, 0, a.length - 1); } private static void sort(Comparable[] a, int lo, int hi){ if (hi <= lo) return; int j = partition(a, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); }}
Time: In general, quick sorting is faster than merge sort, but in adverse circumstances (select Minimum or maximum element), quick sort time is \ (O (n^2) \)
Fast sorting is an unstable sort, the merge sort is a stable sort, the merge sort is used in Java for object, and the quick sort is used when the efficiency is emphasized.
Quick sort speeds up in practice in the following ways:
- Insert sort for sequences with small dividers
- Get median elements from a sample
A quick sort is sensitive to repeating element values, when too many repeating elements are nearly two times the sort process, and the merge sort is relatively insensitive.
The method of dealing with repeating elements is to use the 3-point method (3-way partitioning), that is, select the element a[j], the array is divided into less than, equal to, greater than a[j] 3 parts, 3 method of the time greatly reduced, much smaller than the merge sort
Summary of sorting methods
- |
stable? |
Worst |
Average |
| Best
remarks |
Selection |
- |
\ (n^2/2\) |
\ (n^2/2\) |
\ (n^2/2\) |
N Exchanges |
Insertion |
? |
\ (n^2/2\) |
\ (n^2/4\) |
N |
Use for small N or partially ordered |
Shell |
- |
? |
? |
N |
Tight code, Subquadratic |
Merge |
? |
\ (N*LG (N) \) |
\ (N*LG (N) \) |
\ (N*LG (N) \) |
Guarantee, Stable |
Quick |
- |
\ (n^2/2\) |
\ (2n*ln (N) \) |
\ (N*LG (N) \) |
\ (N*LG (N) \) Probabilistic guarantee fastest in practice |
3-way Quick |
? |
\ (n^2/2\) |
\ (2n*ln (N) \) |
N |
Holy sorting Grail |
Application
Array Reset Shuffle
- Assigns the corresponding random number to each element of the array, sorts the random number and swaps the array elements to achieve the reset effect.
- Knuth Shuffle, get 0 to n two random number i,j, Exchange A[i] and A[j]
Select Selection
Select the element of the row K.
In the way of class quick sorting, randomly select elements to group, if the left element is k-1, then return the selected element directly, if the left element is less than k-1, then the selected element on the right, on the right to continue repeating selection, otherwise on the left repeat selection process.
public static Comparable select(Comparable[] a, int k){ StdRandom.shuffle(a); int lo = 0, hi = a.length - 1; while (hi > lo){ int j = partition(a, lo, hi); if (j < k) lo = j + 1; else if (j > k) hi = j - 1; else return a[k]; } return a[k];}
Convex bag (convex hull)
In a real vector space V, for a given set X, all the intersection s of the convex sets that contain x are called convex packages of x.
Algorithm:
There are currently hundreds of sorting algorithms, here are the most used to select a few
Reference Princeton University Algorithm I
Summary of sorting algorithms