original article, reprint please indicate from Iron Man Mac blog http://www.cnblogs.com/gangtiexia The basic idea of a
direct insert sort (straight insertion sort) is to insert a new record into an ordered table that is already sorted, that the initial ordered table has only the first data of the unordered table, and sequentially inserts each data into the unordered table in order to get an ordered table , the specific steps are
- If new record < ordered table High L.r[j], set Sentinel
- Orderly table back move, j+1=j
- Repeat the 2nd step until the new record >= the J record in the ordered table, then j+1 is the position to be inserted.
Thus, a new sequential table with a 1 increase in the number of records is obtained.
Demo Example:
C language Implementation (compiler dev-c++5.4.0, source code suffix. cpp)
1#include <stdio.h>2 3typedeffloatKeyType;4typedefstruct{5 KeyType score;6 Charname[ -];7 }student;8typedefstruct{9 intLength=6;TenStudent stu[6]; One }sqlist; A - voidSIS (SqList &m) { - for(intI=2; i<l.length;i++){ the if(l.stu[i].score>l.stu[i-1].score) { -l.stu[0]=l.stu[i];//Set Sentinel - intJ; - for(j=i-1; l.stu[0].score>l.stu[j].score;j--) +l.stu[j+1]=L.STU[J];//record and move back -l.stu[j+1]=l.stu[0];//find location, insert element + } A } at } - - intMain () { - sqlist L; - - for(intI=1; i<l.length;i++){ inprintf"\ n Please enter the first name of the student%d:", i); - gets (l.stu[i].name); toprintf"Score:"); +scanf"%f",&(L.stu[i].score)); - GetChar (); the } * $ SIS (L);Panax Notoginseng for(intI=1; i<l.length;i++){ -printf"\ n Student%s score%f%d", l.stu[i].name,l.stu[i].score,i); the } +}
C-language implementation of direct insertion sequencing (straight insertion sort)