The insertion sort is divided into 4 types:
Direct Insert Sort, binary insert sort, 2-way insert sort, table Insert sort, hill sort.
Let's introduce each of the following:
Direct Insert Sort
Process Narrative: The 1th record in the sequence is first considered an ordered subsequence, and then inserted one after another from the second record until the entire sequence becomes a non-descending ordered sequence by the keyword. I pass the direct insert sort operation as: Insert record R[i] into ordered subsequence {r[1],r[2]......r[i-1]} (search forward from i-1, and then move back to record until the insertion point is found, insert), into an ordered subsequence containing I records {r[1],r[2]......r[ I-1]}.
Note:
1. Insert success one value for one trip.
2. Need to set up a lookout at R[0].
3. The entire sequencing process is n-1 inserted.
Algorithm program:
voidInsertsort (SqList &l) {//Order table L do direct insert sort (ascending) for(i=2; i<=l.length; i++) {if(L.r[i].key < l.r[i-1].key) {l.r[0]=l.r[i];//Set up a lookoutl.r[i]=l.r[i-1];//To r[i-1] to move back for(j=i-2; l.r[0].key < L.r[j].key; j--) {//Because the first if statement has already judged r[i-1] and R[i] (Sentinel) of the large //small, and move back to the operation, thus starting from r[i-2]. J in the last store to //The subscript of the insertion point. l.r[j+1]=L.R[J];//Does not meet the criteria, move back. } l.r[j+1]=l.r[0];//To insert} }}
Evaluation :
1. Direct Insert sort comparison and number of moves is approximately (n^2)/4 time complexity is O (x^2).
2. Applies to n very small cases.
Binary Insert Sort
Binary lookup principle is very simple, that is, direct insertion in the process of direct lookup process with binary search instead, that is, reduce the number of comparisons.
Algorithm Code:
voidBinsertsort (SqList &l) {//Order table L for binary insert sort. for(i=2; i<=l.length; i++) {//r[i] into an ordered sequence of r[i~ (i-1)]l.r[0]=l.r[i];//Set up a lookoutlow=1; high=i-1; while(Low<=high) {//Find the insertion pointM= (Low+high)/2;if(l.r[0].key < L.r[m].key) {high=m-1; }Else{low=m+1; } } for(j=i-1; j>=high+1; j--) {l.r[j+1]=L.R[J];//Record move back} l.r[high+1]=l.r[0];//Insert}}
Evaluation :
1. Binary insertion sorting only reduces the number of comparisons, and the number of records moved is inconvenient. Therefore, the time complexity is still O (x^2).
2. Suitable for when n is relatively large.
2-Way Insertion sort
principle:
The 2-way insertion sort is improved on the basis of the binary insertion sort to reduce the number of moves recorded during the sorting process. To do this, you need a secondary space of N records.
Process Description :
Set another array d with the same type as L.R, first assigns l.r[1] to d[1], and D[1] as a record in an intermediate position in the ordered sequence, and then from the second record in L.R sequentially into a sequential sequence before or after the d[1].
Through an auxiliary loop array, if it is greater than the largest element, it is inserted into the tail, and if it is less than the smallest element, it is inserted into the head,
If between the two, using binary find the way to move a part of the element;
Demo
Note :
- Consider D as a loop array. (The so-called loop array does not mean that they really end-to-end, but rather logically loops.) Completed by%)
Replace x with (x+n)%n to complete the loop.
- Set two pointer first and final respectively to indicate the position of the last record in D in the ordered sequence obtained during the sorting process.
Algorithm Program :
voidTworoadinsertsort (int*arr,int*temp,intN) {//arr is the original array, temp is temporary, n is array length intI, First,FinalK First =Final=0; temp[0] = arr[0];//assigns the first element of arr to temp for(i =1; I < n; i + +) {if(Arr[i] < Temp[first]) {//The element to be inserted is smaller than the smallest elementFirst = (First-1+ N)% n; Temp[first] = Arr[i]; }Else if(Arr[i] > temp[Final]) {//To insert an element larger than the largest element Final= (Final+1+ N)% n; temp[Final] = Arr[i]; }Else{//Insert element is larger than the smallest, smaller than the maximum (this procedure can be added to the last element in the previous demonstration 26, go through) Note: It is best to find binary!!! K = (Final+1+ N)% n; while(Temp[(K-1) + N)% n] > Arr[i]) {temp[(k + N)% n] =temp[(K-1+ N)% n]; K = (K-1+ N)% n; } temp[(k + N)% n] = arr[i];Final= (fianl +1+ N)% n; } }//Copy the sorted records to the original order table for(k =0; K < n; K + +) {Arr[k] = temp[(first + K)% n]; } }
Evaluation :
2-Way Insert sort the end-to-end insertion does not require moving elements. The number of moves is approximately (n^2)/8, which reduces the number of moves and does not prevent movement.
Table Insert Sort
Introduction Background : Reduce the number of moves on the basis of 2-way insert sorting until no movement is required.
(not to be continued ...) )
Internal sort (i) Insert sort