Write a C++ program to implement shell sorting algorithm.
Program Code:
#include<iostream>
using namespace std;
void shellSort(int sort[],int m)
{
for(int g = m/2; g > 0; g /= 2)
{
for(int i=g ; i<m ; i++)
{
int temp = sort[i];
int j;
for(j=i; j>=g && sort[j-g]>temp; j-=g)
{
sort[j]=sort[j-g];
}
sort[j]=temp;
}
}
}
int main()
{
int n;
cout<<"Enter the size of the array :";
cin>>n;
cout<<endl;
int sort[n];
cout<<"Enter elements into the array are : ";
for(int i=0;i<n;i++)
{
cin>>sort[i];
}
shellSort(sort,n);
cout<<"Array after sorting is :";
cout<<endl;
for(int i=0;i<n;i++)
{
cout<<sort[i]<<" ";
}
cout<<endl;
return 0;
}
The program code is tested on www.jdoodle.com
Output:
Enter the size of the array : 6
Enter elements into the array are : 87 15 34 99 56 44
Array after sorting is :
15 34 44 56 87 99
Thanks
Mukesh Rajput
Post A Comment:
0 comments: