The characteristics and implementation of the direct insertion of the [C language] insertion sort
1. Algorithm characteristics
Direct insertion is a simple, stable insertion ordering method with the time complexity of O (n), the worst O (N2), the average O (N2), and the Space complexity O (1).
2, algorithm ideas:
In ascending order, for example, set a temporary variable to store the inserted value that will be moved, and then compare it to its previous data from backward to forward. When the comparison value is larger than the insertion value, the comparison value moves one bit, the insertion value continues to be retrieved, and when the comparison value is less than or equal to the insertion value, the insertion value is inserted into the next bit of the comparison value. All the data can be arranged in an orderly fashion after a round of loops.
3. Implementation code
1#include <stdio.h>2 3 //Insert Sort: Insert directly4 voidInsert_sort (intArr[],intlen)5 {6 //Insert this element of the subscript I into the front front array is ordered7 for(intI=1; i<len; i++)8 {9 //record the value to be inserted the backward process overrides the value of Arr[i] to save in advanceTen intnum =Arr[i]; One //Cycle Comparison A intj =0; - for(j=i-1; j>=0; j--) - { the if(Num <Arr[j]) - { -arr[j+1] = Arr[j];//move the data backwards . - } + Else //arr[j] >= num inserted position j+1 - { + Break; A } at } - if(J! = I1) - { -arr[j+1] =num; - } - } in } - to voidTravelintArr[],intlen) + { - for(intI=0; i<len;i++) the { *printf"#df", Arr[i]); $ } Panax Notoginsengprintf"\ n"); - } the + intMain () A { the intArr[] = { -, the,9,233, +, -, -,9,4, the}; + intLen =sizeof(arr)/sizeof(arr[0]); - $ Travel (Arr,len); $ Insert_sort (Arr,len); - Travel (Arr,len); - the /*Travel (Arr,len); - Binary_insert_sort (Arr,len);Wuyi Travel (Arr,len);*/ the - /*Travel (Arr,len); Wu Shell_sort (Arr,len); - Travel (Arr,len);*/ About}
4. Test results
The characteristics and implementation of the direct insertion of the [C language] insertion sort