**C program of Shell Sort Algorithm** : Shell sort is one of the oldest **sorting algorithms**. Shell sort is a multi-pass algorithm which generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange.

Lets see how the sort a list of elements using shell sort :

- Arrange the data sequence in a two-dimensional array
- Sort the columns of the array

The effect of above two sequence is that, the data sequence is partially sorted. The process above is repeated, but each time with a narrower array, i.e. with a smaller number of columns. In the last step, the array consists of only one column. In each step, the sortedness of the sequence is increased, until in the last step it is completely sorted. However, the number of sorting operations necessary in each step is limited, due to the pre-sortedness of the sequence obtained in the preceding steps.

Lets us see how to write a **C program of shell sort algorithm** which accepts number of elements and their values as input and displays the sorted list as output.

### C program of Shell Sort Algorithm :

#include<stdio.h> #include<conio.h> void shellsort(int a[],int n) { int j,i,k,m,mid; for(m = n/2;m>0;m/=2) { for(j = m;j< n;j++) { for(i=j-m;i>=0;i-=m) { if(a[i+m]>=a[i]) break; else { mid = a[i]; a[i] = a[i+m]; a[i+m] = mid; } } } } } int main() { int a[20],i,n; printf("-----------------------------------------------------------\n"); printf("----------------------Made by C code champ-----------------\n"); printf("-----------------------------------------------------------\n\n"); printf("\t\tSHELL SORT ALGORITHM\n\n\n\n"); printf("Enter total elements : "); scanf("%d", &n); printf("\nEnter %d Elements : ", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); shellsort(a,n); printf("\n\n\nSorted list by Shell sort :\n "); for(i=0;i< n;i++) printf("%d\t",a[i]); getch(); return 0; }

We hope that you all have enjoyed the **C program** of Shell Short Algorithm. If you have any queries or issues with the above code, ask us in form of comments.