The basic idea of a direct insert sort (insertion sort) is:
Each time a record is sorted, the size of its keyword is inserted into the appropriate position in the previously ordered subsequence until all records have been inserted.
Set the array to a[0...n-1]:
1. Initially, a[0] self into 1 ordered areas, unordered area is a[1..n-1]. Make I=1
2. Merge A[i] into an ordered interval of A[0...I] formed in the current ordered region A[0...i-1].
3. i++ and repeat the second step until the i==n-1. Sorting is complete.
#include <stdio.h>#include<stdbool.h>voidInsertsort (int*a,intN) {inti,j,k; for(i =1; I < n; i++) { for(j = i-1; J >=0; j--){ if(A[j] < a[i]) Break; } if(J! = i-1) { inttemp =A[i]; for(k = i-1; K >j; k--) A[k+1] =A[k]; A[k+1] =temp; } }}voidPrintArray (int*a,intN) { for(inti =0; I < n; i++) {printf ("%d", A[i]); } printf ("\ n");}intMain () {inta[7] = {5,2,1,8,Ten, at, A}; Insertsort (A,7); PrintArray (A,7); return 0;}
The binary insertion sort (binary insertion sort) is an improvement to the insertion sorting algorithm, which, because of the sequencing algorithm, inserts the elements sequentially into the sequence that is already sorted. Since the first half of the sequence is already ordered, we do not need to find the insertion point sequentially, and we can use the binary lookup method to speed up the search for the insertion point.
Algorithmic thinking
In the process of inserting a new element into an ordered array, when looking for the insertion point, set the first element of the insertion area to A[low] and the end element to A[high], then the wheel will be inserted into the element with A[m], where m= (Low+high)/2 is compared, if larger than the reference element, Select A[low] to a[m-1] for the new insert area (i.e. high=m-1), otherwise select a[m+1] to A[high] for the new insert area (i.e. low=m+1), so that until the Low<=high is not established, all elements after this position will be moved back one bit, and insert the new element into a[high+1].
Stability and complexity
The binary insertion sorting algorithm is a stable sorting algorithm, which significantly reduces the number of comparisons between keywords compared to the direct insertion algorithm, so the speed is faster than the direct insert sorting algorithm, but the number of records moved does not change, so the time complexity of the binary insertion sorting algorithm is still O (n^2), and the same as the direct insertion sorting algorithm. Additional space O (1).
#include <stdio.h>#include<stdbool.h>voidBinaryinsertsort (int*a,intN) {intI, J, K, Low, High, m; for(i =1; I < n; i++) { low=0; High= i-1; while(Low <=High ) {m= (low + high)/2; if(A[m] > a[i]) high = M-1; ElseLow = M +1; } if(J! = i-1) { inttemp =A[i]; for(k = i-1; K >= High +1; k--) A[k+1] =A[k]; A[k+1] =temp; } }}voidPrintArray (int*a,intN) { for(inti =0; I < n; i++) {printf ("%d", A[i]); } printf ("\ n");}intMain () {inta[7] = {5,2,1,8,Ten, at, A}; Binaryinsertsort (A,7); PrintArray (A,7); return 0;}
Http://www.cnblogs.com/archimedes/p/4014912.html
Data structure--sort--direct insert sort and binary insertion sorting algorithm