Wednesday, 18 March 2015

Program to sort the given numbers using shell sort Using C


# include <stdio.h>
# include <conio.h>
void main()
{
int i, j, k, n, a[10], t ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = (n + 1) / 2 ; i >= 1 ; i = i / 2)
for(j = i ; j < n ; j++)
{
t = a[j] ;
for(k = j - i ; k >= 0 && t < a[k] ; k = k - i)
a[k + i] = a[k] ;
a[k + i] = t ;
}
printf("\nThe sorted elemets are :\n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter the limit : 5
Enter the elements :
20 40 30 50 10
The sorted elemets are :
10 20 30 40 50

No comments:

Post a Comment