Direct insertion sorting, semi-insertion sorting, and 2-way insertion sorting

Source: Internet
Author: User

According to the book,SortIt is an important operation in computer programming. Its function is to rearrange any sequence of data elements (or records) into a sequence ordered by keywords.

To facilitate sorting of integer arrays. Sort the array elements in ascending order. What record and structure sorting algorithms will be used in the future. Only the implementation is provided here. For more information about the algorithm, see Yan Weimin's book.


// Insert the sorting directly

Void insertsort (INT array [], size_t size) // The array is empty and serves as the Sentinel. Size is the length of the array {for (INT I = 2; I <size; ++ I) {If (array [I] <array [I-1]) {array [0] = array [I]; array [I] = array [I-1]; Int J; For (j = I-2; array [0] <array [J]; -- j) {array [J + 1] = array [J];} // For array [J + 1] = array [0];} // If} //}

// Semi-insert sorting

void  BInsertSort(int array[],size_t size){for (size_t i=1;i<size;i++){int tem=array[i];int low=0,high=i-1;while (low<=high){int m=(low+high)/2;if (tem<array[m]){high=m-1;}else low=m+1;}//whilefor (int j=i-1;j>=high+1;--j){array[j+1]=array[j];}array[high+1]=tem;}//for}     


// 2-Sort Inserts

Void twayinsertsort (INT array [], size_t size) {int first, final; first = final = 0; int TEM [size]; TEM [0] = array [0]; for (INT I = 1; I <size; I ++) {If (array [I]> = TEM [Final]) {TEM [++ Final] = array [I];} else if (array [I] <= TEM [first]) {First = (first-1 + size) % size; TEM [first] = array [I];} else // performs semi-insertion sorting. Many people have directly inserted this part on the Internet. {Int low, high; If (array [I] <tem [0]) {LOW = first; high = size-1;} else {LOW = 0; high = final;} int m; while (low <= high) {M = (low + high)/2; If (array [I] <tem [m]) high = m-1; else low = m + 1;} For (Int J = final; J! = High; j = (J-1 + size) % size) {TEM [(j + 1) % size] = TEM [J];} TEM [(high + 1) % size] = array [I]; Final ++;} // else} // forfor (INT I = 0, j = first; I <size; j = (J + 1) % size, ++ I) array [I] = TEM [J];}

// 2-another version of the sort of path insertion

void  BInsertSort(int array[],size_t size){for (size_t i=1;i<size;i++){int tem=array[i];int low=0,high=i-1;while (low<=high){int m=(low+high)/2;if (tem<array[m]){high=m-1;}else low=m+1;}//whilefor (int j=i-1;j>=high+1;--j){array[j+1]=array[j];}array[high+1]=tem;}//for}

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.