Showing posts with label Program - Shell sort. Show all posts
Showing posts with label Program - Shell sort. Show all posts

Saturday 16 March 2013

Program - Shell sort


Program - Shell sort

// Shell  sort

#include<stdio.h>
#include<conio.h>

void shell_sort(int[],int);
void output(int [],int);

void main()
{
     int values[50],n,i,j,temp;
     clrscr();

     printf("How Many Elements You Want To Enter ?  :");
     scanf("%d",&n);

     for(i=0;i<n;i++)
       {
 printf("\n Enter any number : ");
 scanf("%d",&values[i]);
       }

       printf("\n List before sorting");
       output(values,n);
             
       shell_sort(values,n);

       printf("\n list after sorting ");
       output(values,n);
       getch();
}
void output(int values[],int n)
{
int i;
for(i=0;i<n;i++)
{
 printf("\n  %d",values[i]);
}

}
void shell_sort(int array[],int size)
{
  int temp,gap,i,swap;

  gap=size/2;

  do
   {

     do
       {
        swap=0;
  
  
      for(i=0;i<size-gap;i++)
        {

if(array[i]>array[i+gap])
           {
                   temp=array[i];
                   array[i]=array[i+gap];
                   array[i+gap]=temp;
                   swap=1;
           }

    }
}while(swap);
  }while(gap=gap/2);
}