Shell-Sort

SAHIL SIDDIQUI
5 min readMar 24, 2021

--

Shell sort is often termed as an improvement over insertion sort. In insertion sort, we take increments by 1 to compare elements and put them in their proper position.

In shell sort, the list is sorted by breaking it down into a number of smaller sublists. It’s not necessary that the lists need to be with contiguous elements. Instead, the shell sort technique uses increment i, which is also called “gap” and uses it to create a list of elements that are “i” elements apart.

Shell sort is an algorithm that first sorts the elements far apart from each other and successively reduces the interval between the elements to be sorted. It is a generalized version of insertion sort.

In shell sort, elements at a specific interval are sorted. The interval between the elements is gradually decreased based on the sequence used. The performance of the shell sort depends on the type of sequence used for a given input array.

Some of the optimal sequences used are:

  • Shell’s original sequence: N/2 , N/4 , …, 1
  • Knuth’s increments: 1, 4, 13, …, (3k — 1) / 2
  • Sedgewick’s increments: 1, 8, 23, 77, 281, 1073, 4193, 16577…4j+1+ 3·2j+ 1
  • Hibbard’s increments: 1, 3, 7, 15, 31, 63, 127, 255, 511…
  • Papernov & Stasevich increment: 1, 3, 5, 9, 17, 33, 65,…
  • Pratt: 1, 2, 3, 4, 6, 9, 8, 12, 18, 27, 16, 24, 36, 54, 81….

How Shell Sort Works?

General Algorithm:-

The general algorithm for shell sort is given below.

shell_sort (A, N)
where A — list to be sorted; N — gap_size

set gap_size = N, flag = 1
while gap_size > 1 or flag = 1, repeat
begin
set flag = 0
set gap_size = (gap_size + 1)/2
end
for i = 0 to i< (N-gap_size) repeat
begin
if A[i + gap_size] > A[i]
swap A[i + gap_size], A[i]
set flag = 0
end
end

Thus in the above algorithm, we first set N which is the gap for sorting the array A using shell sort. In the next step, we divide the array into sub-arrays by using the gap. Then in the next step, we sort each of the sub-arrays so that at the end of the loop we will get a sorted array.

Next, let us consider a detailed example to better understand the shell sort using a pictorial representation.

Suppose, we need to sort the following array.

Initial array

1. 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.

If the 0th element is greater than the 4th one then, the 4th element is first stored in the 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

In the second loop, an interval of N/4 = 8/4 = 2 is taken and again the elements lying at these intervals are sorted.

Rearrange the elements at n/4 interval

Rearrange the elements at n/4 interval

All the elements in the array lying at the current interval are compared.

1. The elements at the 4th and 2nd positions are compared. The elements at the 2nd and 0th position are also compared. All the elements in the array lying at the current interval are compared.

The same process goes on for the remaining elements.

Rearrange all the elements at n/4 interval

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.

Rearrange the elements at n/8 interval

Output:

Array to be sorted:

9 8 3 7 5 6 4 1

Array after shell sort:

1 3 4 5 6 7 8 9

C++ Example:-

Let us see the implementation of shell sort in C++.

Complexity:-

Shell sort is an unstable sorting algorithm because this algorithm does not examine the elements lying in between the intervals.

Time Complexity

  • Worst Case Complexity: less than or equal to O(n2)
    Worst-case complexity for shell sort is always less than or equal to O(n2).

    According to Poonen Theorem, worst-case complexity for shell sort is Θ(Nlog N)2/(log log N)2) or Θ(Nlog N)2/log log N) or Θ(N(log N)2) or something in between.
  • Best Case Complexity: O(n*log n)
    When the array is already sorted, the total number of comparisons for each interval (or increment) is equal to the size of the array.
  • Average Case Complexity: O(n*log n)
    It is around O(n1.25).

The complexity depends on the interval chosen. The above complexities differ for different increment sequences chosen. Best increment sequence is unknown.

Space Complexity:

The space complexity for shell sort is O(1).

Conclusion

Shell sort is the highly efficient algorithm that comes to an improvement over the insertion sort.

While insertion sort works by incrementing its elements by 1, shell sort uses the parameter “gap” to divide the array into sub-arrays whose elements are “gap” apart. Then we can sort the individual list using insertion sort to obtain the complete sorted array.

Shell sort performs faster than insertion sort and takes fewer moves to sort the array when compared to insertion sort. Our upcoming tutorial will explore all about the heap sort technique for sorting data structures.

Shell Sort Applications

Shell sort is used when:

  • calling a stack is overhead. uClibc library uses this sort.
  • recursion exceeds a limit. bzip2 compressor uses it.

Insertion sort does not perform well when the close elements are far apart. Shell sort helps in reducing the distance between the close elements. Thus, there will be fewer swappings to be performed.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

SAHIL SIDDIQUI
SAHIL SIDDIQUI

No responses yet

Write a response