See this blog for details: http://www.cnblogs.com/skywang12345/p/3596881.html
Introduction to direct insertion sorting
The basic idea of a direct insert sort (straight insertion sort) is to look at the N-sorted elements as an ordered table and an unordered table. The ordered table contains only 1 elements at the beginning, and the unordered table contains n-1 elements, each time the first element is taken out of the unordered table, it is inserted into the appropriate position in the ordered table, the order is made into a new ordered table, and the sorting process is repeated n-1 times.
Complete code for subset completion:
#include"stdio.h"/** Direct Insert Sort * 2015-08-10:may * parameter Description: * A--array to be sorted * N--The length of the array*/voidInsert_sort (intA[],intN) { intI, j, K; for(i=1; i<n;i++) { for(j=i-1; j>=0; j--) { if(a[i]>A[j]) { Break;//Find a location, find a place larger than a certain location and stop . } } if(j!=i-1)//The position is found, and the back of the position is started, and if it happens, it doesn't move . { intTemp=a[i];//the first to be inserted will be overwritten. for(k=i-1; k>j;k--) {a[k+1]=A[K];//all move backwards .} a[j+1]=temp;//insert in; } }}voidMainvoid){ inta[]={5,8,3,1, A,1}; Insert_sort (A,6); for(intk=0;k<6; k++) {printf ("%d", A[k]); }}
Data structure _ Direct insertion sorting method