Showing posts with label Implementation of Shell Sort. Show all posts
Showing posts with label Implementation of Shell Sort. Show all posts

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;
}}}