Reference 1) <Algorithms_4th> Chapter 2 Sorting
Reference 2) sorting algorithms, Wiki
1 Selection Sort Selection
First, the whole sequence is compared once, the minimum value is selected, and the first position is placed;
Then, the remaining sequence is compared again, the minimum value is selected, and the second position is placed; And so on
Java instance:
Public classselection{ Public Static voidsort (comparable[] a) {//Sort a[] into increasing order. intN = A.length;//Array Length for(inti = 0; i < N; i++) { //Exchange A[i] with smallest entry in A[I+1...N). intmin = i;//index of minimal entr. for(intj = i+1; J < N; J + +) if(Less (A[j], a[min])) min =J; Exch (A, I, min); } }}View Code
2 Insertion Sort Insert sorting
First of all, the first two elements comparison, reverse exchange position, the order is unchanged;
The subsequent elements are then compared with them (that is, the preceding elements that have been sorted), the corresponding positions are inserted, and so on.
Java instance:
Public classinsertion{ Public Static voidsort (comparable[] a) {//Sort a[] into increasing order. intN =a.length; for(inti = 1; i < N; i++) { //Insert A[i] among a[i-1], a[i-2], a[i-3] ... .. for(intj = i; J > 0 && less (a[j], a[j-1]); j--) Exch (A, J, J-1); } }}View Code
3 merge sort combined sorting
4 Quick Sort Fast sorting
5 Bubble sort Bubble sorting
First, the first two elements are compared, the small value is placed before, the large value is discharged;
Then, move one position to the right and continue the 22 comparison until the maximum value sinks to the end;
Sorting algorithm Visualization