Implementation of shell sort using C in dataStructure with example developerIndian.com

Updated:13/08/2023 by Computer Hope

Go Back



Want to learn how we can implement shell sort in c

The generalisation of insertion sort, known as shell sort, compares elements separated by a gap of many locations, so addressing insertion sort's shortcomings.

  • It is a sorting algorithm that is an extended version of insertion sort.
  • Shell sort has improved the average time complexity of insertion sort.
  • As similar to insertion sort, it is a comparison-based and in-place sorting algorithm.
  • Shell sort is efficient for medium-sized data sets.

          //WAP to array using shell sort method
#include<stdio.h>
#include<conio.h>
void shellsort(int *a,int size)
 {
 int temp,gap,i,swap;
 gap=size/2;
 do{
    do{
       swap=0;
       for(i=0;i<size-gap;i++)
	 { if(a[i]>a[i+gap])
	     {
	     temp=a[i];
	     a[i]=a[i+gap];
	     a[i+gap]=temp;
	     swap=1;
	     }
	 }
      }while(swap);
   }while(gap=gap/2);
 }
void main()
 {
 int x[10],i;
 printf("\n enter array ");
 for(i=0;i<10;i++)
 scanf("%d",&x[i]);
 shellsort(x,10);
 printf("\n after sorting");
 for(i=0;i<10;i++)
    printf("  %d",x[i]);
}

        

Conclusion

In this article , The Shell Sort algorithm is implemented with this shellsort  function. It begins with a big gap and closes it till it is just 1. After that, it sorts each gap value using an insertion algorithm.An array is initialised, the unsorted array is printed, the shellSort function is called to sort the array, and the sorted array is printed.