original article, reprint please indicate from Iron Man Mac blog Http://www.cnblogs.com/gangtiexiaThe basic idea of
2-way insertion sort (2-way insertion sort) :elements smaller than fisrt, inserted in front of first;an element larger than final, inserted after final,larger than the fisrt and smaller than the final element inserted in the middleDemo 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 - voidTwo_wayis (SqList &L) { - SqList temp; - intfirst=1, final=1; +temp.stu[first]=l.stu[1]; - for(intI=2; i<l.length;i++){ + if(L.stu[i].score>temp.stu[final].score) {//Insert Final back Atemp.stu[i]=L.stu[i]; atfinal++; -}Else if(L.stu[i].score<temp.stu[first].score) {//Insert first front - if(first==1) first--;//array starts with 1 -First= (first-1+l.length)%l.length;//First change to 1->5->4>3>2 ... -temp.stu[first]=L.stu[i]; -}Else if(L.stu[i].score<temp.stu[final].score) {//plug in the middle in intP= (final-1+l.length)%l.length; - if(p==1) p--; to while(l.stu[i].score<Temp.stu[p].score) { +P= (P-1+l.length)%l.length; - if(p==1) p--; the } *final++; $ intk=final;Panax Notoginseng while(K> (p+1)){ -temp.stu[k]=temp.stu[k-1]; thek= (K-1+l.length)%l.length; + if(k==1) k--; A } thetemp.stu[p+1]=L.stu[i]; + - } $ } $ } - - intMain () { the sqlist L; - Wuyi for(intI=1; i<l.length;i++){ theprintf"\ n Please enter the first name of the student%d:", i); - gets (l.stu[i].name); Wuprintf"Score:"); -scanf"%f",&(L.stu[i].score)); About GetChar (); $ } - - Two_wayis (L); - A for(intI=1; i<l.length;i++){ +printf"\ n Student%s score%f%d", l.stu[i].name,l.stu[i].score,i); the } - return 1; $}
2-Way Insertion sort (2-way insertion sort) for C language implementation