When analyzing the process of inserting sorting algorithm performance, it is known that when the array is small or there are more ordered subsequence, the insertion sort will finish sorting the array in a very short time, so you can design a monotone sequence h[n], divide the array into smaller sequences, and then use the insertion sort for these small sequences. H[n]={1,4,7,10,13,16,19......,3*x+1}.
Algorithm implementation:
void Sort::shell_sort (int* A, const int n) {int h = 0;while (H<N/3) H = 3*h + 1;while (h>=1) {for (int i=h; i<n; i++) { for (int j=i; j>=h && a[j] < a[j-h]; j = j-h) {swap (a,j,j-h);}} h = H/3;}}
Algorithm analysis: From the implementation of the above algorithm can be seen, hill sorting is mainly divided into a number of small array of arrays, and then the small array to insert the order, after this order, the entire array will have a number of local ordered sub-series, so that the array is used (h=1) to insert the sort, The sorting of the array will be completed very quickly.
Algorithm----Hill sort (shell sort)