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
- First record exists l.r[0], low= ordered table low subscript, high= ordered table high subscript
- 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
- Loop 2nd step, then high+1 is the position to insert
- 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