Shell Sort Algorithm
Shell Sort Algorithm
Shell Sort Algorithm
Shell sort is a generalized version of the insertion sort algorithm. It first sorts elements that are far
apart from each other and successively reduces the interval between the elements to be sorted.
The interval between the elements is reduced based on the sequence used. Some of the optimal
sequences that can be used in the shell sort algorithm are:
Note: The performance of the shell sort depends on the type of sequence used for a given input
array.
2. Initial array
3. We are using the shell's original sequence (N/2, N/4, ...1) as intervals in our algorithm.
In the first loop, if the array size is N = 8 then, the elements lying at the interval of N/2 =
4 are compared and swapped if they are not in order.
b. If the 0th element is greater than the 4th one then, the 4th element is first stored
in temp variable and the 0th element (ie. greater element) is stored in
the 4th position and the element stored in temp is stored in the 0th position.
4. In the second loop, an interval of N/4 = 8/4 = 2 is taken and again the elements lying at these
intervals are sorted.
Rearran
ge the elements at n/4 interval
5. All the elements in the array lying at the current interval are compared.
The elements at 4th and 2nd position are compared. The elements at 2nd and 0th position
are also compared. All the elements in the array lying at the current interval are compared.
6. The same process goes on for remaining elements.
Rearran
ge all the elements at n/4 interval
7. Finally, when the interval is N/8 = 8/8 =1 then the array elements lying at the interval of 1
are sorted. The array is now completely sorted.
Rearran
ge the elements at n/8 interval
shellSort(array, size)
end shellSort
Shell Sort Code in C
#include <stdio.h>
// Shell sort
int j;
array[j] = temp;
// Print an array
printf("\n");
// Driver code
int main() {
int data[] = {9, 8, 3, 7, 5, 6, 4, 1};
shellSort(data, size);
printArray(data, size);
Time Complexity
Best O(nlog n)
Worst O(n2)
This C program sorts a given array of integer numbers using Shell Sort technique.
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(n 2) 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.
1. /*
3. */
4. #include <stdio.h>
6. {
7. int i, j, k, tmp;
9. {
10. for (j = i; j < num; j++)
11. {
13. {
15. break;
16. else
17. {
21. }
22. }
23. }
24. }
25. }
27. {
33.
35. {
37. }
42. return 0;
43. }
OUTPUT: