Program to Implement Shell Sort in CPP (C plus plus)
In this tutorial, we will learn about the Program to Implement the Shell Sort in CPP (C plus plus).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include<iostream> using namespace std; void sorting(int a[],int n) { int tmp; int i; int j; int g; for(g=n/2;g>0;g/=2) { for(i=g;i<n;i+=1) { tmp=a[i]; for(j=i;j>=g && a[j-g]>tmp;j-=g) a[j]=a[j-g]; a[j]=tmp; } } } int main() { int array[20]; int i; int num; cout<<"Please enter the number of values: "; cin>>num; cout<<"Please enter values into array "<<endl; for(i=0;i<num;++i) cin>>array[i]; sorting(array,num); cout<<endl<<"Array after shell sorting is: "<<endl; for(i=0;i<num;++i) cout<<array[i]<<" "; return 0; } |
Output:
