Hill sort (Shell sort)
#include <iostream>
using namespace Std;
void print (int a[], int n, int i)
{
Cout<<i << ":";
for (int j= 0; j<n; j + +)
{
COUT<<A[J] << "";
}
cout<<endl;
}
/* Direct insertion of the general form of the sort *
* d Zoom out increment, if is direct insert sort, d=1 */
void Shellinsertsort (int a[], int n, int d)
{
for (int i= D; i<n; ++i)
{
if (A[i] < a[i-d])//If the number of positions where I is greater than the number of i-d position, then directly inserted, otherwise the element moved, make space after inserting
{
int j = i-d;
int x = A[i]; Copy as Sentinel, which stores the elements to be sorted
while (x < a[j])//Find the insertion position in an ordered table
{
A[J+D] = A[j];
J-= D; Element move back
}
A[J+D] = x; Insert to correct position
}
Print (A, n,i);
}
}
/** The hill Sort by the increment D (N/2,n is the number of numbers to be sorted) first **/
void Shellsort (int a[], int n)
{
int d = N/2;
while (d >= 1)
{
Shellinsertsort (A, n, d);
D = D/2;
}
}
int main ()
{
int a[9] = {1,1,5,7,2,4,9,6,8};
int length=sizeof (a)/sizeof (a[0]);
Shellsort (a,length); Hill Insert Sort
Print (a,length,length);
}
Data structure and algorithms--Hill sort (shell sort)