C++ Program for ShellSort

C++ Program for ShellSort :Shell short is an improved and efficient version of the insertion sort.ShellSort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead.

C++ Program for ShellSort

#include<iostream>
using namespace std;
void sort(int a[],int n)
{
int gap,i,j,temp;

for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)
a[j]=a[j-gap];

a[j]=temp;
}
}
}

int main()
{
int a[20],i,n;

cout<<"Enter number of elements:";
cin>>n;

cout<<"Enter array elements:\n";
for(i=0;i<n;++i)
cin>>a[i];

sort(a,n);

cout<<"\nArray after shell sort:\n";
for(i=0;i<n;++i)
cout<<a[i]<<" ";

return 0;
}

Output of Program

Enter number of elements:5
Enter array elements:
24
23
0.

Array after shell sort:
0 0 23 24 1577808