×

Search anything:

Shell Sort

DSA Takeover Cheatsheet

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.


Shellsort (also known as Shell sort or Shell's method) is an in-place comparison based sorting algorithm.

Shell Sort improves its time complexity by taking the advantage of the fact that using Insertion Sort on a partially sorted array results in less number of moves.

It is a generalization of:

  • sorting by exchange (bubble sort)
  • sorting by insertion (insertion sort)

Explanation

  • The idea is to arrange the list of elements so that, starting anywhere, considering every hth element gives a sorted list. Such a list is said to be h-sorted.

  • Note 1-sorted array is a sorted array.

  • Determine a gap sequence to work on.

If the gap sequence is (5,3,1) and the array consists of 14 elements (a1, a2, ..., a14), then, there are three passes.

  • In the first pass, the elements are grouped as (a1, a6, a11), (a2, a7, a12), (a3, a8, a13), (a4, a9, a14), (a5, a10). Each group is sorted using Insertion Sort.
  • In the second pass, the 3-sorting is performed on groups (a1, a4, a7, a10, a13), (a2, a5, a8, a11, a14), (a3, a6, a9, a12).
  • In the third (last) pass, 1-sorting is performed on (a1, a2, ..., a14).

Pseudocode of Shell Sort using Marcin Ciura's gap sequence, with an inner insertion sort:


# Sort an array a[0...n-1].
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
# Start with the largest gap and work down to a gap of 1
foreach (gap in gaps)
{
    # Do a gapped insertion sort for this gap size.
    # The first gap elements a[0..gap-1] are already in gapped order
    # keep adding one more element until the entire array is gap sorted
    for (i = gap; i < n; i += 1)
    {
        # add a[i] to the elements that have been gap sorted
        # save a[i] in temp and make a hole at position i
        temp = a[i]
        # shift earlier gap-sorted elements up until the correct location for a[i] is found
        for (j = i; j >= gap and a[j - gap] > temp; j -= gap)
        {
            a[j] = a[j - gap]
        }
        # put temp (the original a[i]) in its correct location
        a[j] = temp
    }
}

Complexity

  • Worst case time complexity: Θ(N (log N)^2) comparisons
  • Average case time complexity: Θ(N (log N)^2) comparisons
  • Best case time complexity: Θ(N log N)
  • Space complexity: Θ(1).

Implementations

Implementation of Shell Sort algorithm in 8 language that includes C, C++, Java, Python, Go, JavaScript, C# and Swift.

  • C
  • C++
  • Java
  • Python
  • Go
  • JavaScript
  • C#
  • Swift

C


  #include <stdio.h>
// Part of Cosmos by OpenGenus Foundation
void shell_sort(int *a, int n)
{
  int i, j, gap, temp;
  /* Assess the array in gaps starting at half the array size - Each gap is a sublist of 2 values */
  for(gap = n/2 ; gap > 0 ; gap /= 2)
  {
    /* Index at the right value of each gap */
    for(i = gap ; i < n ; i++)
    {
      /* Take a copy of the value under inspection */
      temp = a[i];
      for(j = i ; j >= gap ; j-= gap)
      {
        /* Compare the values in the sub lists and swap where necessary */
        if(temp < a[j-gap])
          a[j] = a[j-gap];
        else
          break;
      }
      a[j] = temp;
    }
  }
}
int main(void)
{
  int i;
  int a[10] = { 5 , 211 , 66 , 7 , 12 , 2, 76 , 134 , 99 , 9 };
  printf("In: ")
  for(i = 0 ; i < 10 ; i++)
  {
    printf("%d ", a[i]);
  }
  shell_sort(a, 10);
  printf("\nOut: ")
  for(i = 0 ; i < 10 ; i++)
  {
    printf("%d ", a[i]);
  }
  return 0;
}

C++

Java

Python

Go

JavaScript

C#

Swift

Applications

Applications of Shell Sort includes:

  • Shellsort performs more operations and has higher cache miss ratio than quicksort.
  • However, since it can be implemented using little code and does not use the call stack, some implementations of the qsort function in the C standard library targeted at embedded systems use it instead of quicksort. Shellsort is, for example, used in the uClibc library. For similar reasons, an implementation of Shellsort is present in the Linux kernel.
  • Shellsort can also serve as a sub-algorithm of introspective sort, to sort short subarrays and to prevent a slowdown when the recursion depth exceeds a given limit. This principle is employed, for instance, in the bzip2 compressor.
Alexa Ryder

Alexa Ryder

Hi, I am creating the perfect textual information customized for learning. Message me for anything.

Read More

Improved & Reviewed by:


Shell Sort
Share this