Direct Insert Sort

Source: Internet
Author: User

1, find the insertion position of L (i) in l[1...i-1] K2, move all elements in l[k...i-1] back one position 3, copy L (i) to L (k)
  
 
  1. void InsertSort(ElemType A[], int n)
  2. {
  3. int i, j;
  4. for(i=2; i<=n; i++)//依次将A[2]~A[n]插入到前面已排序序列
  5. {
  6. if(A[i].key)<A[i-1].key)//如果A[i]的关键码小于其前驱,须将A[i]插入有序表
  7. {
  8. A [ 0 ]= a [ i ; //copy as Sentinel, A[0] does not store element
  9. for(j=i-1; A[0].key<A[j].key; --j)//从后往前查找待插入位置
  10. {
  11. A[j+1]=A[j];//向后挪位
  12. }
  13. A[j+1]=A[0];//复制到插入位置
  14. }
  15. }
  16. }
Time complexity O (n^2) stable sequencing

From for notes (Wiz)

Direct Insert Sort

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.