Program to Implement Shell Sort in CPP (C plus plus)

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).

#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:

Program to Implement Shell Sort in CPP (C plus plus)
Program to Implement Shell Sort in CPP (C plus plus)

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.