Direct insertion sort thinking and code implementation

Source: Internet
Author: User

In the direct insertion sort, in Min's book it is necessary to set the Sentinel, the role of the Sentinel is to avoid the array out of bounds, so in the first position to set Sentinel, after thinking this algorithm for me the most worthy of learning is to compare the location of the move at the same time, this will reduce the complexity of time
Data structure for

#define MAXSIZE 20int KeyType;struct sqList{    KeyType r[MAXSIZE+1];    int length;};

The code in the book containing the Sentinel is as follows

voidSinsertsort (SqList L,intLen) {//There is a very worthwhile place to learn, which is to move directly while comparing    if(len<=1)return; for(intI=2; i<=len;i++) {if(l.r[i-1]>l.r[i]) {l.r[0]=l.r[i];intJ for(j=i-1; l.r[j]>l.r[0];--J)//This prevents the array from oversteppingl.r[j+1]=L.R[J]; l.r[j+1]=l.r[0]; }    }cout<<"with Sentinel's direct insertion sort"<<endl; for(intI=1; i<=l.length;i++)cout<<L.r[i]<<" ";cout<<endl;}

The code in the book is the use of a space in the array itself l.r[0]. For people who have just written code, moving data while comparing data saves time
for (j=i-1; L.R[J]>L.R[0];–J)
L.R[J+1]=L.R[J];
In fact, it's the same with other spaces as Sentinels.

voidSInsertSort1 (SqList L,intLen) {//written by yourself without a sentinel    if(len<=1)return; for(intI=1; i<len;++i) {if(l.r[i-1]>l.r[i]) {intJintTemp=l.r[i]; for(j=i-1; l.r[j]>temp&&j>=0;--j) l.r[j+1]=L.R[J]; l.r[j+1]=temp; }    }cout<<endl;cout<<"No Sentinel's direct insertion sort"<<endl; for(intI=0; i<l.length;i++)cout<<L.r[i]<<" ";cout<<endl;}

The use of j>0 as a sentry in this code is actually the same as the principle in the book, and the reason for this is to make it clear that the Sentinel's meaning lies.

int main () {sqlist L;L. R[1]= the;L. R[2]= -;L. R[3]= $;L. R[4]= the;L. R[5]= the;L. R[6]= -;L. R[7]= -;L. R[8]= the;L. Length=8;SqList L1;L1. R[0]= the;L1. R[1]= -;L1. R[2]= $;L1. R[3]= the;L1. R[4]= the;L1. R[5]= -;L1. R[6]= -;L1. R[7]= the;L1. Length=8;Sinsertsort (L,8);SInsertSort1 (L1,8);    /*binsertsort (l,8); BInsertSort1 (l1,8); * *Return0;}

Time complexity is O (N2) in direct insert sort

In fact, it is not very clear here, if the function void SIS1 (sqlist &l,int len) in the L with the accessor is not written j>=0 run the result is also correct, as if the compiler can automatically determine whether it is out of bounds, but L do not have to take the address, it must have a J >=0, otherwise the array will go out of bounds.

Direct insertion sort thinking and code implementation

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.