The Hill sort (Shell sort) is a kind of insertion sort, which is an improvement on the direct insertion sorting algorithm. This method is also known as narrowing the incremental sort, because of the DL. The shell was named after it was introduced in 1959.
The hill sort is essentially a grouping insertion method. Its basic idea is: for N to sort the series, take an integer less than n Gap (gap is called step) to sort the elements into several groups of sub-sequences, all the distance is a multiple of the gap is placed in the same group; then, the elements within each group are directly inserted into the sort. Once this sequence is complete, the elements of each group are ordered. The gap value is then reduced, and the grouping and sorting are performed repeatedly. Repeat this operation, when the gap=1, the whole sequence is orderly.
#include <iostream>using namespace std;void shellsort (int arr[],int n) {int i,j,gap,k,key;for (gap = n/2;gap > 0;g AP/= 2) {for (i = 0;i < Gap;i + +) {for (j = i + gap;j < N;j + = gap) {k = J;key = arr[k];while (k-gap >= 0 && ARR[K] < Arr[k-gap]) {Arr[k] = arr[k-gap];k-= gap;} ARR[K] = key; }}}}int Main () { int a[10] = {3,6,1,9,4,5,2,7,0,8}; Shellsort (a,10); for (int i = 0;i < 10;i + +) cout<<a[i]<< ""; cout<<endl; return 0;}
Operation Result:
Implementation of Hill Sort algorithm