Simplified notes for the fourth edition of the algorithm! To see for yourself.
Data, an unordered array of length n
Api
Exch ([]a,i,j) Exchange elements of the array I and J positions
Less (I,J) Size: array element i<j?true:false
A choice of sort selection
i=0; Select the minimum value from the I~n and I exchange position; i++; cycle;
Characteristics
The run time is the same as the input (no matter how messy, or the elements are all the same) sort time.
Data movement is minimal, and each element is exchanged only once.
public static void sort (comparable[] a) { int N = A.length; for (int i = 1; i < n; i++ int min = I; for (int j = i; j < N; j++ if (less (A[j], a[min])) { Min = J; }} exch (A, I, min); } }
View Code
Two-insert Sort insertion
Insert I into the appropriate position in the sorted 0~i, i++;
Insertion method: Compare I with i-1, less than swap I with i-1;i--; loop until the appropriate position is inserted
Characteristics
An array that is close to order is much faster than a disorderly or reverse order.
Suitable for small-scale arrays, or local optimizations of large arrays
Improved
The Insert method does not always swap the position, directly locates the position, and all the subsequent elements move right
/*** Sort the LF-RT elements in a not including RT position * *@paramA *@paramLF *@paramRT*/ Public Static voidSort (comparable[] a,intLfintRT) { for(inti = lf + 1; i < RT; i++) { for(intj = i; J > LF && less (a[j], a[j-1]); j--) {Exch (A, J, J-1); } } }
View Code
Three Hill sort shell
Like an insert sort, the insertion sort can be treated as an incremental h=1.
Parameter: Increment h decrements to 1 (h optional, example increment approximately equals N/3)
Will h,2h,3h.
H+i,2h+i,3h+i. N
... Sort by INSERT, respectively
H Decrement (example increment for h/=3)
Cycle, until h<=1;
Public Static voidsort (comparable[] a) {intN =a.length; //Incremental intH = 0; while(H < N/3) H= h * 3 + 1; while(H >= 1) { for(inti = h; i < N; i++) { for(intj = i; J >= H && less (A[j], a[j-h]); J-=h) {Exch (A, I, j); }} H/= 3; } }
View Code
Four merge sort merge
For two stacked cards, just compare the top of the two stacks in turn, and take the smaller one.
The merge is divided into two kinds of list sorting for bottom-up
1 Top down
Will n keep two points, like a binary tree, the last node an element
2 in the combined
2,2 to 4
Until it is n
Public Static voidsort (comparable[] a) {intn =a.length; comparable [] aux=NewComparable[n]; Sort (A,0, N-1, aux); } Private Static voidSort (comparable[] a,intLfintRT, comparable[] aux) { if(LF >=RT)return; if((RT-LF) <=15){ NewInsertion (). Sort (a,lf,rt+1); } intMID = lf + (RT-LF)/2; Sort (A, LF, Mid, aux); Sort (A, mid+ 1, RT, aux); if(Less (A[mid], A[mid + 1])) return; Elsemerge (A, LF, mid, RT, aux); } /*** Merge Lf-mid and Mid+1-rt * *@paramA *@paramLF *@paramMid *@paramRT *@paramaux*/ Private Static voidMerge (comparable[] A,intLfintMidintRT, comparable[] aux) { for(inti = LF; I <= RT; i++) {Aux[i]=A[i]; } inti =LF; intj = Mid + 1; for(intK = LF; K <= RT; k++) { if(I >mid) A[k]= aux[j++]; Else if(J >RT) A[k]= aux[i++]; Else if(Less (Aux[i], aux[j])) A[k]= a[i++]; ElseA[k]= a[j++]; } }
View Code
2 Bottom up
Direct n to 1, similar to n-ary tree
The 2 .... Until it is n
Original sort: The sorted subsequence is merged, a temporary array is required to copy two sub-sequences and then sorted to the original array corresponding position
Improvement: 1 When the subsequence length is less than a certain value, use an insert sort, such as a length of 15
2 The original sort needs to copy the temporary array continuously, can begin to copy to the auxiliary array once more, directly sorts to the sort array? Reduces replication time but does not reduce space
Five Quick Sort
Cond....
Algorithm Summary-sort (fast line not written)