C + + Insert sort two-way insertion (ring versus non-loop comparison)

Source: Internet
Author: User

#include <iostream>using namespace STD;voidGrial (intA[],intN) {intI,j; i=j=0;intB[n]; b[0]=a[i];intK for(k=1; k<n;k++) {if(B[i]<a[k]) {b[i+1]=A[K];            i++; }Else if(B[j]>a[k]) {b[(J-1+n)%n]=a[k]; J= (J-1+n)%n; }Else{i++;intM for(M = i;b[m-1]>a[k];m--) {b[(m+n)%n] = b[(M-1+n)%n];            } b[(M+n)%n]=a[k];    }} i++; K =0; Do{a[k++]=b[(i)%n];    i++; } while(I%N!=J);}intMain () {inta[]={Ten,4,2,5, the,1, at}; Grial (A,7); for(intI=0;i<7; i++) {cout<<a[i]<<"\ T"; }cout<<endl;return 0;}//Experience: Here the 2-way insertion sort, although to some extent reduces the time efficiency of the insertion sort, that is, the consumption of mobile data, but if this is an array in reverse order,//Then it moved as much as the number of times, so I thought of a method like the following, do not use a circular move, I only compare once, and then move, that is, no from 0 subscript to n//Subscript move, then use a balance factor flags will make the insertion more balanced, which will somewhat reduce the special rate, see my analysis code.//The following is my own feeling that the loop is a bit of a hassle, and when the number exceeds 0 subscript, the number of bits moved will be many, the following is my method,//No cyclic movement, but compared to b[0], if smaller than b[0], in comparison Len (i) with Len (J), if the left side is greater than the right, I will insert it to the right. The reverse is similar. #include <iostream>using namespace STD;//2 Road Insert sort.voidGrial (intA[],intN) {intIintJintB[n];//New space for sorting.b[0]=a[0]; I=1; j=n-1;intK for(k=1; k<n;k++) {intFlag = i> (n-j)?1:-1;//Compare the left length with the right length, if the left is large, then flag 1,else -1.        if(a[k]>b[i-1]) {b[i]=a[k];        i++; }Else if(a[k]<b[0] && flag = =1)//a[k]<b[0], we can insert a[k] to the left of b[0], or to the j-n position,                                                                        //Make the length of the two sides in the B array almost identical. {if(j==n-1) {B[j]=a[k]; j--;Continue; } for(intm = j;m<n;m++) {if(a[k]>b[m+1]) b[m]=b[m+1];Else{B[m]=a[k]; Break;        }} j--; }Else{intM for(M = i; b[m-1]>a[k] && m>0; m--) {b[m]=b[m-1];            } B[m]=a[k];        i++;    }} j = i; k=0; Do{a[k++]=b[(i+n)%n];        i++; } while((i%n)!=j);}intMain () {inta[]={2,4, the,3,1,6,3,Ten,8,-1}; Grial (A,Ten); for(intI=0;i<Ten; i++) {cout<<a[i]<<"\ T"; }cout<<endl;return 0;}

C + + Insert sort two-way insertion (ring versus non-loop comparison)

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.