1. the process of inserting a sort is like the way we usually play poker to get the cards inserted, constantly inserting the cards that have been taken out into place.
The initial ordered interval size of the insertion sort process is 1, the first element of the disordered interval is taken out, and the appropriate position of the ordered interval is searched for insertion. Repeat the above process to complete the operation.
Illustration example
1 //Insert Sort2 //Karllen @20153 voidInsertsort ()4 {5 intI, J, temp;6 for(i =1; i<n;++i)//Insert starting from the second element7 {8temp = A[i];//A[i] will be overwritten, temporarily saved9j = i-1;Ten while(j>=0&&A[J]>TEMP)//Edge Move Edge comparison One { Aa[j+1] =A[j]; - } -a[j+1] =temp; the } -}
2. The shell sorting algorithm is one of the insertion sorting algorithms, and the hill sort first divides the sorted set of data into groups by one increment, and the incremental elements make up a set of separate
Insert a sort, and then zoom out incrementally , repeating the process. Until the increment is reduced to 1 o'clock, the entire result of the order can be sorted into a group and inserted into the sort to complete.
Illustration example:
Take increment group as {3,2,1};
The increment is 3 o'clock, which is divided into three groups {70,10,90,60},{30,80,100,45},{40,20,75} , respectively, and the result is
{10,60,70,90}
{30,45,80,100}
{20,40,75}; The result is a local order enhancement of {10,30,20,60,45,40,70,80,75,90,100} after the grouping of increments of 3 is executed after the sort is inserted.
Increment is 2 o'clock,{10,30,40,60,45,20,70,80,75,90,100} is divided into two groups , {10,20,45,70,75,100},{30,60,40,80,90} is inserted in sort , the result is
{10,20,45,70,75,100}
{30,40,60,80,90}; The result is a further enhancement of the local ordering of {10,30,20,40,45,60,70,80,75,90,100} after the execution of a grouping of increments of 2 is inserted.
The last execution of the direct insert sort with an increment of 1, with the result {10,20,30,40,45,60,70,75,80,90,100} to the end of the shell sort.
Important: The process of grouping for insert sorting is intended to make the data move in a locally ordered direction.
This involves the selection of increments , the selection of the increment should theoretically be 22 mutual.
The code is as follows:
#include <iostream>/*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*///Karllen// - voidShellsort (int*a,intlength);//Hill Sort intMainintargcChar**argv) { intA[] = { -, -, +,Ten, the, -, -, -, the, -, $}; Shellsort (A, One); for(inti =0;i< One;++i) {std::cout<<a[i]<<" "; } return 0;}voidShellsort (int*a,intLength//Hill Sort{ for(intINCRT = length/2; Incrt>0; INCRT/=2) { for(inti =0; i<incrt;++i)//sort the sub-sequences of different increments in sequence { /*int j = I+INCRT; int R; while (j<length)//Normal insert sort, first compare after move {r = i; while (A[j]>a[r]) {r = r+incrt; } if (r!=j) {int temp = a[j]; int k = J; while (k>=r) {a[k] = A[k-incrt]; K-= INCRT; } A[r] = temp; } j = J+incrt; } */ //Improved direct insert sort, edge move edge comparison. intj = i+INCRT; intr,temp; while(j<length) {R= JINCRT; Temp=A[j]; while(r>=0&& a[r]>temp)//moving edge from rear to front comparison{a[r+INCRT] =A[r]; R= RINCRT; } a[r+INCRT] = temp;//Insertj = j+incrt;//the insertion process to the next element } } } }
Test results:
The book to Time to hate less, is not known difficult. Boguan and about, thick and thin hair. @karllen make a little progress every day.
Insert sort and shell sort (Hill sort)