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}