Shellsort, also known as Shell sort or Shell’s method, is an in-place comparison sort. It can either be seen as a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting elements far apart from each other and progressively reducing the gap between them. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange. Worst case time complexity is O(n2) and best case complexity is O(nlog(n)).
Here is the source code of the C program to sort integers using Shell Sort technique. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to sort an array using Shell Sort technique
void shellsort(int arr, int num)
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
for (j = i; j < num; j++)
for(k = j - i; k >= 0; k = k - i)
if (arr[k+i] >= arr[k])
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
int k, num;
printf("Enter total no. of elements : ");
printf("\nEnter %d numbers: ", num);
for (k = 0 ; k < num; k++)
printf("\n Sorted array is: ");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
$ gcc shellsort.c -o shellsort $ ./shellsort Enter total no. of elements : 10 Enter numbers : 36 432 43 44 57 63 94 3 5 6 Sorted array is : 3 5 6 36 43 44 57 63 94 432
Sanfoundry Global Education & Learning Series – 1000 C Programs.