Shell Sort Using C

Description

 This program is used to sort elements using Shell Sort.


#include<stdio.h>
void sort();
main()
{
 int choice;
 do
 {
  printf("\n----MENU----\n1.Enter elements\n2.EXIT\n");
  printf("Enter your choice\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:sort();break;
   case 2:printf("\n----Thank you----\n");break;
   default:printf("\nINVALID INPUT !!!!");
  }
 }while(choice!=2);
}

void sort()
{
 int i,j,temp,n,gap,flag;
 int a[100];
 printf("Enter no. of elements to be sorted\n");
 scanf("%d",&n);
 printf("\nEnter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 gap=n+1/2;
 while(gap>0)
 {
  i=0;
  while((gap+i)<n)
  {
   if(a[i]>a[i+gap])
   {
    temp=a[i];
    a[i]=a[i+gap];
    a[i+gap]=temp;
   }i++;
  }
  gap--;
 }
 printf("\n\nSorted Elements are :\n");
 for(i=0;i<n;i++)
 {
  printf("%d\n",a[i]);
 }
}


OUTPUT

Shell Sort Output 1 Shell Sort Output 2

Labels: