You are on page 1of 8

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:

 Shell's original sequence: N/2 , N/4 , …, 1

 Knuth's increments: 1, 4, 13, …, (3k – 1) / 2

Note: The performance of the shell sort depends on the type of sequence used for a given input
array.

Working of Shell Sort

1. Suppose, we need to sort the following 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.

a. The 0th element is compared with the 4th element.

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.

Rearrange the elements at n/2 interval


This process goes on for all the remaining elements.
Rearrange all the elements at n/2 interval

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

Shell Sort Algorithm

shellSort(array, size)

for interval i <- size/2n down to 1

for each interval "i" in array

sort all the elements at interval "i"

end shellSort
Shell Sort Code in C

// Shell Sort in C programming

#include <stdio.h>

// Shell sort

void shellSort(int array[], int n) {

// Rearrange elements at each n/2, n/4, n/8, ... intervals

for (int interval = n / 2; interval > 0; interval /= 2) {

for (int i = interval; i < n; i += 1) {

int temp = array[i];

int j;

for (j = i; j >= interval && array[j - interval] > temp; j -= interval) {

array[j] = array[j - interval];

array[j] = temp;

// Print an array

void printArray(int array[], int size) {

for (int i = 0; i < size; ++i) {

printf("%d ", array[i]);

printf("\n");

// Driver code

int main() {
int data[] = {9, 8, 3, 7, 5, 6, 4, 1};

int size = sizeof(data) / sizeof(data[0]);

shellSort(data, size);

printf("Sorted array: \n");

printArray(data, size);

Shell Sort Complexity

Time Complexity  

Best O(nlog n)

Worst O(n2)

C Program to Implement Shell Sort(VERSION2):

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. /*

2.   * C Program to sort an array using Shell Sort technique

3.   */

4. #include <stdio.h>

5. void shellsort(int arr[], int num)

6. {

7. int i, j, k, tmp;

8. for (i = num / 2; i > 0; i = i / 2)

9. {
10. for (j = i; j < num; j++)

11. {

12. for(k = j - i; k >= 0; k = k - i)

13. {

14. if (arr[k+i] >= arr[k])

15. break;

16. else

17. {

18. tmp = arr[k];

19. arr[k] = arr[k+i];

20. arr[k+i] = tmp;

21. }

22. }

23. }

24. }

25. }

26. int main()

27. {

28. int arr[30];

29. int k, num;

30. printf("Enter total no. of elements : ");

31. scanf("%d", &num);

32. printf("\nEnter %d numbers: ", num);

33.  

34. for (k = 0 ; k < num; k++)

35. {

36. scanf("%d", &arr[k]);

37. }

38. shellsort(arr, num);

39. printf("\n Sorted array is: ");

40. for (k = 0; k < num; k++)


41. printf("%d ", arr[k]);

42. return 0;

43. }

OUTPUT:

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

You might also like