2-way insertion sorting

Source: Internet
Author: User
Void cinsertionsort: path2insertion (void) {// element 0 is a sentinel. Const int COUNT = 9, length = count-1; int L [count] = {0, 49, 38, 65, 97, 76, 13, 27, 49 }; // sort the sequence table L by 2-path insertion. Int d [length] = {0}; d [0] = L [1]; // The first record of D in L is the record with the sorting order in D. Int first = 0, final = 0; // first and final indicate the positions of the first and last records in the order of the records in D. For (INT I = 2; I <= length; ++ I) // sort the 2nd ~ Insert the last record to D. {If (L [I] <D [first]) // The value of the record to be inserted is smaller than the minimum value in D and before d [first] (the element of D array does not need to be moved ). {First = (first-1 + length) % length; d [first] = L [I];} else if (L [I]> d [Final]) // if the value of the record to be inserted is greater than the minimum value in D, insert the record to d [Final] (the element of D array does not need to be moved ). {Final = final + 1; d [Final] = L [I];} else // The number of records to be inserted must be greater than the minimum value in D and less than the maximum value in D, insert it to the center of D (Elements of D array need to be moved ). {Int J = final ++; // move the end element of D to insert records in sequence. While (L [I] <D [J]) {d [(j + 1) % length] = d [J]; j = (J-1 + length) % length;} d [J + 1] = L [I] ;}} for (INT I = 1; I <= length; I ++) // cyclically assigns d to L. {L [I] = d [(I + first-1) % length]; // linear relationship.} // Print the sorting result. For (INT I = 0; I <= length; ++ I) {cout <L [I] <"\ t" ;}cout <Endl ;}

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.