Saturday, April 20, 2013

Shell Sort



#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<windows.h>

using namespace std;


void shellsort(int a[],int n)
{
     int j,i,k,m,mid;
     for(m = n/2;m>0;m/=2)
     {
           for(j = m;j< n;j++)
           {
                 for(i=j-m;i>=0;i-=m)
                 {
                                     if(a[i+m]>=a[i])
                                                     break;
                                     else
                                     {
                                         mid = a[i];
                                         a[i] = a[i+m];
                                         a[i+m] = mid;
                                     }
                 }
           }
     }
}

int main()
{
    int a[15]={12,6,19,7,45,21,50,15,38,40,36,72,90,80,32},i;
    int b[15]={12,6,19,7,45,21,50,15,38,40,36,72,90,80,32};
    system("cls");
    cout<<"\n\t   SHELL-SORTING\n\n";
       
    printf("\nBefore\t: ");
  
   
    for(i=0;i<15;i++)
    {cout<<a[i]<<"  ";
    shellsort(a,15);
    }
    cout<<"\n-----------------------------------------------------------------------";
    printf("\nAfter\t: ");
    for(i=0;i<15;i++)
   
    {
    cout<<a[i]<<"  ";
    }
getch();
return 0;
}


No comments:

Post a Comment