Binary Insert Sort

Source: Internet
Author: User

Similar to the direct insertion sort, but with binary lookup when looking for an ordered sub-table
  
 
  1. void InsertSort(ElemType A[], int n)
  2. {
  3. int i, j, low, high, mid;
  4. for(i=2; i<=n; i++)//依次将A[2]~A[n]插入到前面已排序序列
  5. {
  6. A[0]=A[i];//复制为哨兵,A[0]不存放元素
  7. low=1;//设置折半查找的范围
  8. high=i-1;
  9. while(low<=high)//折半查找,默认是递增有序
  10. {
  11. mid=(low+high)/2;
  12. if(A[mid].key>A[0].key)//查找左半边子表
  13. high=mid-1;
  14. else//查找右半边子表
  15. low=mid+1;
  16. }
  17. Span class= "KWD" >for ( j = i - 1 ; J >= high + 1 -- j )
  18. A[j+1]=A[j];//统一后移元素,空出插入位置
  19. A[high+1]=A[0];//插入操作
  20. }
  21. }



From for notes (Wiz)

Binary 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.