Data structure and algorithm--Direct Insert Sort

Source: Internet
Author: User

Haven't written a blog for a long time, today simple summary under the direct insertion sort.

What is a direct insert sort?

inserting a sort directly is to compare an element to an element in the order table and then insert it in the appropriate position so that the entire order table is in an orderly state. the book on the structure of statistics will be used to illustrate the analogy of poker, just like playing poker, when we get a few cards, to be organized into a straight son, we usually do is to put the small card, in front of the big cards. In particular, you can read the book, think about it, or it is easy to understand.

the idea of inserting the order directly : Each time an element to be sorted is inserted into an ordered sequential sequence with the size of its key code.

The direct insert sort has two basic questions:

    • How to construct an initial ordered sequence.
    • How to find the insertion point in an ordered sequence.

Specific Implementation :

<span style= "Font-family:courier new;font-size:14px;" > #include <iostream> #define MAXSIZE 10using namespace std;struct sqlist{    int r[maxsize+1];//to store the array to sort r[ 0] used as sentinel    int length;   The length of the sequential table};void Insertsort (sqlist *l) {    int J;    for (int i=2;i<=l->length;i++) {        //element comparison        if (L->r[i]<l->r[i-1]) {            l->r[0] = l->r[i];/ /assign this element to Sentinel for            (j=i-1; l->r[j]>l->r[0];j--) {                l->r[j+1] = l->r[j];//move back            }            l->r[j+1] = l->r[0];  Place the inserted element at the specified position        }    }}void Print (sqlist *l) {for    (int i=1;i<=l->length;i++) {        cout<<l- >r[i]<< "";    }} int main () {    SqList L = {{0,6,8,3,11,7},5};    Insertsort (&l);    Print (&l);    return 0;} </span>


Data structure and algorithm--Direct Insert 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.