Open In App

Shell Sort - Python

Last Updated : 06 Nov, 2025
Comments
Improve
Suggest changes
7 Likes
Like
Report

Shell Sort is an improvement over Insertion Sort. Instead of comparing adjacent elements, it compares elements that are far apart using a gap. The gap keeps reducing until it becomes 1, at which point the list is fully sorted. This allows elements to move faster toward their correct positions.

Algorithm Steps

  1. Initialize the gap as half the length of the array (n/2).
  2. Divide the array into smaller sublists, each separated by the gap.
  3. Perform insertion sort within these sublists.
  4. Reduce the gap size and repeat sorting until the gap becomes 0.
  5. Stop when the array is completely sorted.

Working Example

Let’s take a small list for example: [9, 1, 5, 3]. Below is the step-by-step process:

Initial list: [9, 1, 5, 3], Number of elements = 4 → start with gap = 2.

Pass 1 (gap = 2):

  • Compare arr[0] (9) and arr[2] (5): since 9 > 5 → swap → [5, 1, 9, 3]
  • Compare arr[1] (1) and arr[3] (3): since 1 < 3 → no swap → Result after gap=2: [5, 1, 9, 3]

Reduce gap: gap = 1 (regular insertion sort step)

Pass 2 (gap = 1):

  • Compare arr[1] (1) with arr[0] (5): 1 < 5 → swap → [1, 5, 9, 3]
  • Compare arr[2] (9) with arr[1] (5): 9 > 5 → no swap
  • Compare arr[3] (3) with arr[2] (9): 3 < 9 → swap → [1, 5, 3, 9]
  • Compare arr[2] (3) with arr[1] (5): 3 < 5 → swap → [1, 3, 5, 9]. Result after gap=1: [1, 3, 5, 9] (sorted).

Python Implementation

Using Ciura Sequence

This method uses a predefined sequence of gaps based on experimental results (701, 301, 132, 57, 23, 10, 4, 1). The array is first sorted using large gaps and then progressively smaller gaps, ensuring that elements move closer to their correct positions faster.

Python
def shell_ciura(a):
    n = len(a)
    gaps = [701, 301, 132, 57, 23, 10, 4, 1]
    for g in gaps:
        if g >= n: 
            continue
        for i in range(g, n):
            t = a[i]
            j = i
            while j >= g and a[j-g] > t:
                a[j] = a[j-g]
                j -= g
            a[j] = t

arr = [12, 34, 54, 2, 3]
shell_ciura(arr)
print(arr)

Output
[2, 3, 12, 34, 54]

Explanation:

  • gaps = [...] predefined Ciura gaps (large -> small) and for g in gaps: iterate each gap (skip gaps ≥ n).
  • for i in range(g, n): for each index in the gapped group and t = a[i]; j = i hold current value and index.
  • while j >= g and a[j-g] > t: shift larger g-spaced elements right.
  • a[j] = t insert value into correct gapped position and repeat for next gap until gap = 1 completes final pass.

Using Knuth Sequence

This method generates gaps dynamically using the formula gap = 3*gap + 1, starting from 1 and going up until it’s less than the array size. Sorting is then performed for each gap in decreasing order, gradually refining the order until the list becomes fully sorted.

Python
def shell_knuth(a):
    n = len(a)
    g = 1
    while g < n//3:
        g = 3*g + 1
    while g > 0:
        for i in range(g, n):
            t = a[i]
            j = i
            while j >= g and a[j-g] > t:
                a[j] = a[j-g]
                j -= g
            a[j] = t
        g //= 3

arr = [12, 34, 54, 2, 3]
shell_knuth(arr)
print(arr)

Output
[2, 3, 12, 34, 54]

Explanation:

  • g = 1 then while g < n//3: g = 3*g + 1 build initial Knuth gap and while g > 0: iterate gaps from large to small.
  • for i in range(g, n): process each element in a g-spaced group and t = a[i]; j = i store value and index.
  • while j >= g and a[j-g] > t: shift elements that are greater by gap.
  • a[j] = t place element in correct gapped spot and g //= 3 reduce gap per Knuth rule, repeat.

Using Half-gap Division

This traditional method starts with a gap equal to half of the array length and keeps halving the gap until it becomes 1. At each stage, elements that are gap positions apart are compared and rearranged, resulting in a gradually sorted list.

Python
def shell_half(a):
    n = len(a)
    g = n // 2
    while g > 0:
        for i in range(g, n):
            t = a[i]
            j = i
            while j >= g and a[j-g] > t:
                a[j] = a[j-g]
                j -= g
            a[j] = t
        g //= 2

arr = [12, 34, 54, 2, 3]
shell_half(arr)
print(arr)

Output
[2, 3, 12, 34, 54]

Explanation:

  • g = n // 2 start with half the array length and while g > 0: continue until gap reduces to 0.
  • for i in range(g, n): iterate each position in the gapped set.
  • t = a[i]; j = i store element and index and while j >= g and a[j-g] > t: shift larger g-spaced elements right.
  • a[j] = t insert element into its gapped place and g //= 2 reduce gap and repeat.

Please refer complete article on ShellSort for more details!


Explore