Thursday, April 19, 2012

Program in C for Shell Sort

/*****<soeasyprograms.blogspot.in>*****/ 
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void shellsort(int a[],int n);
void main()
{
int i,n,a[10];
clrscr();
printf("Enter array size");
scanf("%d",&n);
printf("Enter elements of array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
shellsort(a,n);
printf("Sorted elements");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\n");
getch();
}
void shellsort(int a[],int n)
{
int d,i,temp;
d=n/2;
while(d>=1)
{
for(i=0;i<n-d;i++)
{
if(a[i]>a[i+d])
{
temp=a[i];
a[i]=a[i+d];
a[i+d]=temp;
}
}
if(d==1)
return;
d=d/2.0+0.5;
}
}


Output of this Program is:









2 comments: