Sunday, 17 June 2012

Shell sort


#include<stdio.h>
#include<conio.h>
void main()
{
 int a[20],n,i; void shell(int a[],int n);
 clrscr();
 printf("\n Enter the size of the array ");
 scanf("%d",&n);
 printf("\n Enter the elements: ");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 shell(a,n);
 printf("\n Elements after sorting:");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);
 getch();
}
void shell(int a[],int n)
{
 int j,p,k,temp;
 for(k=n/2;k>0;k=k/2)
 {
  for(p=k;p<n;p++)
  {
   temp=a[p];
   for(j=p; j>=k && a[j-k]>temp;j=j-k)
    a[j]=a[j-k];
    a[j]=temp;
  }
 }
}

No comments:

Post a Comment