shell sort in c

An inventor called DL Shell came up with a very interesting sort which he called shell sort. This sorting algorithm is almost similar to the bubble sort algorithm. The shell sort compares elements that are a certain distance away (d positions away) from each other and it compares these elements repeatedly (bubble sort only compares adjacent elements.) It uses the equation d = (n + 1) / 2.
The comparison model makes the sorting process of the shell sort very efficient

PROGRAM:

/*c program for sorting array using shell sorting method*/
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30];
int i,j,k,tmp,num;
printf(“Enter total no. of elements : “);
scanf(“%d”, &num);
for(k=0; k<num; k++)
{
printf(“\nEnter %d number : “,k+1);
scanf(“%d”,&arr[k]);
}
for(i=num/2; i>0; i=i/2)
{
for(j=i; j<num; j++)
{
for(k=j-i; k>=0; k=k-i)
{
if(arr[k+i]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf(“\t**** Shell Sorting ****\n”);
for(k=0; k<num; k++)
printf(“%d\t”,arr[k]);
getch();
return 0;
}

OUTPUT:

/*****************OUTPUT*****************
Enter total no. of elements : 7
Enter 1 number : 8
Enter 2 number : 3
Enter 3 number : 7
Enter 4 number : 9
Enter 5 number : 1
Enter 6 number : 24
Enter 7 number : 2
**** Shell Sorting ****
1  2  3  7  8  9  24
*****************************************/

related links….