1. Algorithm Introduction
Each time the first element is taken out of the unordered table, it is inserted into the proper position of the ordered table so that the ordered table remains orderly.
2. Algorithm principle
First pass compares the first two numbers, then inserts the second number by the size into the ordered table;
The second one is to scan the third data with the first two numbers, and insert the third number into the ordered table by size;
Proceed sequentially, the entire sequencing process is completed after the (n-1) scan.
3. Source code
C:
1 voidInsertsort (int[] Array) {2 for(intI=1; i<array.length;i++)3 {4 if(array[i]<array[i-1])5 {6 inttemp=Array[i];7 intK = i-1;8 for(intj=k;j>=0&& temp<array[j];j--)9 {Tenarray[j+1]=Array[j]; Onek--; A } -array[k+1]=temp;//Insert the value of the I-bit - } the } -}
4. Complexity of Time:
O (n^2)
5. Stability analysis
It's stable.
Direct Insert Sort