The characteristics and implementation of the direct insertion of the [C language] insertion sort

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.