GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Shell Sort - Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Shell Sort - Python

Last Updated : 6 Nov, 2025

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!

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.