Updated:13/08/2023 by Computer Hope
Go BackWant 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.
//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]);
}
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.