2-Way Insertion sort (2-way 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 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

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.