Shell Sort In Data Structure Using C++

Data Structure Using C++

Array Data Structure

Sorting

Shell Sort

void shellSort(int arr[], int n);
int main(){
 int arr[]={12,11,13,5,6,4,10,3,14,2};
 int n=sizeof(arr)/sizeof(arr[0]); 
 shellSort(arr,n);
 for(int i=0;i<n;i++){
  cout<<arr[i]<<"\t";
 }
}
void shellSort(int arr[], int n)
{
    for (int gap=n/2; gap>0; gap/=2)
    {
     cout<<"gap="<<gap<<endl;
        for (int i=gap; i<n; i+=1)
        {
         cout<<"\ti="<<i<<endl;
   for(int j=i;j>=gap;j-=gap){
         cout<<"\t\tj="<<j<<endl;
    if(arr[j]<arr[j-gap]){
     int temp=arr[j];
     arr[j]=arr[j-gap];
     arr[j-gap]=temp;
    }else{
     break;
    }
   }      
   cout<<endl;
        }
 for(int i=0;i<n;i++){
  cout<<arr[i]<<"\t";
 }
        cout<<endl;
    }
}

Let me know in the comment section if you have any question.

Previous Post:
Insertion Sort In Data Structure Using C++


Comments