1. Direct Insert Sort
1) Time complexity:T (n) =o (n^2);
2) Spatial complexity:S (n) =o (1);
3) Introduction: Direct insertion Sorting is one of the most basic methods of inserting sorting, the basic idea of inserting a sort directly (straight insertion sorting) is to look at the elements of N to be sorted into an ordered table and an unordered table, starting with The ordered table contains only one element, and the unordered table contains n-1 elements, each time the first element is removed from the unordered table, it is inserted into the appropriate position in the ordered table, and the order is repeated n-1 times to complete the sorting process. Insert A[i] into the a[0],a[1],...,a[i-1] the specific implementation of the process is: first assign the a[i] to the variable t, and then T and A[i-1],a[i-2],... Compare, move an element that is larger than t to the right one position until a J (0<=j<=i-1) is found, so that a[j]<=t or J is (-1) and the T is assigned to A[J+1].
4) Direct Insert Sort implementation:
Eg: the sequence to be sorted is {48,62,35,77,55,14,35,98};
A) first to be sorted into the array a[], and a[0] is not assigned (A[0] is a sentinel, its role is to temporarily save the record to be inserted, the second is to prevent the cross-border), first of all the first number (that is, a[1] as the order of the single-element sub-set);
{48},62,35,77,55,14,35,98
B) Next a[2] is assigned to A[0], and then a[2] compared to a[1], each comparison if A[2] is less than a[1] value exchange Otherwise this comparison terminates, after the exchange, A[1] and a[0] compared ( we can see no matter a[1] original a[2],a[0] equals the original A [2], so in any case will not be exchanged with a[0], this is a[0] the role of the whistle and record) in this case a[2]=62> (a[1]=48), so do not exchange;
{48,62},35,77,55,14,35,98;
Similarly:
C) {35,48,62},77,55,14,35,98;
D) {35,48,62,77},55,14,35,98;
E) {35,48,55,62,77},14,35,98;
F) {14,35,48,55,62,77},35,98;
G) {14,35,35,48,55,62,77},98;
H) {14,35,35,48,55,62,77,98};
4) Source code:
typedef int KEYTYPE;
typedef struct{
Keytype key;
Othertype Other_data;
}recordtype;
void Inssort (recordtype r[],int length)
{
for (i=2;i<=length;
{
R[0]=r[i];j=i-1;
while (R[0].key<r[i].key)
{
R[j+1]=r[j];j=j-1;
}
R[J+1]=R[0];
}
}
Insert class sort (direct insert sort)