C-language implementation of direct insertion sequencing (straight insertion sort)

Source: Internet
Author: User

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
    1. If new record < ordered table High L.r[j], set Sentinel
    2. Orderly table back move, j+1=j
    3. 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)

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.