Wednesday, 20 August 2014

C Program to Implement Shell Sort



Shell sorting was first introduced by Donald Shell.It generalized as exchanging sort, such as bubble or insertion sorting, by allowing the comparison and exchange of elements that lie far apart. The running time
of Shell sort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem.

Example of C Program to Implement Shell Sort


#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("%dt",arr[k]);
 getch();
 return 0;
}

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