Shell Sort in DSA C++

Program 1

// Shall Sort Implementation 
#include<iostream>
#define clrscr() system("cls")
using namespace std;
int main()
{
       int a[500],n,i,j,gap,temp;
       clrscr();
       xyz:cout<<"\nEnter the limit of array";
       cin>>n;
       if(n>500)
       {
          cout<<"\nInvalid limit enter again";
            goto xyz;
       }     
         else
         {
               cout<<"Enter elements in array";
               for(i=0;i<n;i++)       
                 cin>>a[i];
              for(gap=n/2;gap>=1;gap=gap/2)    
              {
                   for(j=gap;j<n;j++)                
                   {
                      for(i=j-gap;i>=0;i=i-gap)     
                      {
                               if(a[i+gap]<a[i])
                               {
                                   temp=a[i+gap];
                                   a[i+gap]=a[i];
                                   a[i]=temp;
                               }   
                              else
                               break;
                      }
                   }    
              }
            cout<<"\n Sorted elements: \n"; 
            for(i=0;i<n;i++)
              cout<<a[i]<<"\n";
         }   
          

       return 0;
}

 

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *