Wednesday, February 18, 2015

Implementation of Shell Sort

AIM:
          To write a C program to implement shell sort

ALGORITHM:
            Step 1: Start the program.
Step 2:  Set up a inc number. inc number is set according to given elements in the   
             list.
            Step 3:  Mark each elements which is comes in inc element.
Step 4: Sort marking elements such as smallest to greater is set as left to right and 
            not change remain element.
Step 5: Reduce inc number to one
            Step 6: Repeat step 2,3 and 4 till all the elements not sorted.
Step 7: Stop the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
void shell(int a[],int n);
int i,j,k,n,temp,array[25];
void main()
{
clrscr();
printf("\n SHELL SORT");
printf("\n **********");
printf("\n Enter the limit:");
scanf("%d",&n);
printf("\n Enter the elements\n\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
shell(array,n);
printf("\n Sorted list:");
for(i=0;i<n;i++)
printf("\n %d",array[i]);
getch();
}
void shell(int a[],int n)
{
for(i=(n+1)/2;i>=1;i/=2)
{
for(j=i;j<=n-1;j++)
{
temp=a[j];
k=j-i;
while(k>=0&&temp<a[k])
{
a[k+i]=a[k];
k=k-i;
}
a[k+i]=temp;
}}}

OUTPUT
 Enter the limit: 5
Enter the elements
5
4
3
2
 1
Sorted list: 1
2
3
4
5




RESULT: Thus a C program is written to implement shell sort and executed successfully

No comments:

Post a Comment