Binary insertion Sort (Binary insertion sort) for C language implementation

Source: Internet
Author: User

original article, reprint please indicate from Iron Man Mac blog Http://www.cnblogs.com/gangtiexiaThe basic idea of binary insert sort (Binary insertion sort) is to insert a new record into an ordered table that is already sorted, the initial ordered table has only the first data of the unordered table, sequentially binary the order of each data in the unordered table, and thus obtains the ordered table. The specific steps are
    1. First record exists l.r[0], low= ordered table low subscript, high= ordered table high subscript
    2. If Low<=high, compare l.r[0] with mid= (Low+high)/2-bit data, if L.R[0]>L.R[MID], low=mid+1, if L.r[0]<l.r[mid], then high=mid-1
    3. Loop 2nd step, then high+1 is the position to insert
    4. Repeat steps 1th, 2, and 3 for each new record to get an ordered table

Demo Example: C language Implementation (compiler dev-c++5.4.0, source code suffix. cpp)
1#include <stdio.h>2 #defineLEN 63 4typedeffloatKeyType;5 6typedefstruct{7 KeyType score;8     Charname[ -];9 }student;Ten  Onetypedefstruct{ A     intLength=LEN; - student Stu[len]; - }sqlist; the  - voidBinaryis (SqList &m) { -     intLow,mid,high; -      for(intI=2; i<l.length;i++){ +l.stu[0]=L.stu[i]; -low=1, high=i-1; +          while(low<=High ) { AMid= (Low+high)/2; at             if(l.stu[0].score>L.stu[mid].score) -high=mid-1; -             Else -Low=mid+1; -         } -l.stu[high+1]=l.stu[0]; in     }     - } to  + intMain () { - sqlist L; the  *      for(intI=1; i<l.length;i++){ $printf"\ n Please enter the first name of the student%d:", i);Panax Notoginseng gets (l.stu[i].name); -printf"Score:"); thescanf"%f",&(L.stu[i].score)); + GetChar (); A     }     the      + Binaryis (L); -      $      for(intI=1; i<l.length;i++){ $printf"\ n Student%s score%f%d", l.stu[i].name,l.stu[i].score,i); -     } -     return 1; the}

Binary insertion Sort (Binary insertion sort) for C language implementation

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.