Shell Sort (original) (raw)

Last Updated : 20 Dec, 2025

Shell Sort, also known as Shell's method, is an in-place comparison sort and an optimization of Insertion Sort. It improves upon the efficiency of Insertion Sort by allowing elements to be moved over larger distances in the initial stages, which significantly reduces the number of swaps required, especially for larger datasets.

Steps used in the algorithm are,

  1. Choose a gap sequence (commonly n/2, n/4, ... , 1).
  2. Sort elements at each gap using Insertion Sort.
  3. Reduce the gap and repeat until the gap becomes 1.

Let's sort the list [22, 34, 25, 12, 64, 11, 90, 88, 45]using Shell Sort :

C++ `

#include #include using namespace std;

void shellSort(vector& arr) { int n = arr.size();

// Start with a large gap, then reduce it step by step
for (int gap = n / 2; gap > 0; gap /= 2) {

    // Perform a "gapped" insertion sort for this gap size
    for (int i = gap; i < n; i++) {
        
        // Current element to be placed correctly
        int temp = arr[i];  
        int j = i;

        // Shift elements that are greater than temp to make space
        while (j >= gap && arr[j - gap] > temp) {
            arr[j] = arr[j - gap];
            j -= gap;
        }

        // Place temp in its correct location
        arr[j] = temp;
    }
}

}

// Utility function to print the vector void printArray(const vector& arr) { for (int num : arr) cout << num << " "; cout << endl; }

int main() { vector arr = {12, 34, 54, 2, 3};

shellSort(arr);
printArray(arr);

return 0;

}

Java

public class GFG {

public static void shellSort(int[] arr) {
    int n = arr.length;

    // Start with a large gap, then reduce it step by step
    for (int gap = n / 2; gap > 0; gap /= 2) {

        // Perform a "gapped" insertion sort for this gap size
        for (int i = gap; i < n; i++) {
            
            // Current element to be placed correctly
            int temp = arr[i]; 
            int j = i;

            // Shift earlier elements that are greater than temp
            while (j >= gap && arr[j - gap] > temp) {
                arr[j] = arr[j - gap];
                j -= gap;
            }

            // Place temp in its correct position
            arr[j] = temp;
        }
    }
}

// Utility function to print array
public static void printArray(int[] arr) {
    for (int num : arr) {
        System.out.print(num + " ");
    }
    System.out.println();
}

public static void main(String[] args) {
    int[] arr = {12, 34, 54, 2, 3};
    
    shellSort(arr);
    printArray(arr);
}

}

Python

def shell_sort(arr): n = len(arr)

gap = n // 2
while gap > 0:

    # Perform a "gapped" insertion sort for this gap size
    for i in range(gap, n):
        
        # Current element to be placed correctly
        temp = arr[i]   
        j = i

        # Shift earlier elements that are greater than temp
        while j >= gap and arr[j - gap] > temp:
            arr[j] = arr[j - gap]
            j -= gap

        # Place temp in its correct position
        arr[j] = temp

    # Reduce the gap
    gap //= 2

Utility function to print array

def print_array(arr): print(" ".join(map(str, arr)))

if name == "main": arr = [12, 34, 54, 2, 3]

shell_sort(arr)
print_array(arr)

C#

using System;

class GFG { public static void ShellSort(int[] arr) { int n = arr.Length;

    // Start with a large gap, then reduce it step by step
    for (int gap = n / 2; gap > 0; gap /= 2)
    {
        // Perform a "gapped" insertion sort for this gap size
        for (int i = gap; i < n; i++)
        {   
            // Current element to be placed correctly
            int temp = arr[i]; 
            int j = i;

            // Shift earlier elements that are greater than temp
            while (j >= gap && arr[j - gap] > temp)
            {
                arr[j] = arr[j - gap];
                j -= gap;
            }

            // Place temp in its correct position
            arr[j] = temp;
        }
    }
}

// Utility function to print array
public static void PrintArray(int[] arr)
{
    foreach (int num in arr)
    {
        Console.Write(num + " ");
    }
    Console.WriteLine();
}

public static void Main()
{
    int[] arr = { 12, 34, 54, 2, 3 };

    ShellSort(arr);
    PrintArray(arr);
}

}

JavaScript

function shellSort(arr) { let n = arr.length;

// Start with a large gap, then reduce it step by step
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {

    // Perform a "gapped" insertion sort for this gap size
    for (let i = gap; i < n; i++) {
        
        // Current element to be placed correctly
        let temp = arr[i]; 
        let j = i;

        // Shift earlier elements that are greater than temp
        while (j >= gap && arr[j - gap] > temp) {
            arr[j] = arr[j - gap];
            j -= gap;
        }

        // Place temp in its correct position
        arr[j] = temp;
    }
}

}

// Utility function to print array function printArray(arr) { console.log(arr.join(" ")); }

// Driver code let arr = [12, 34, 54, 2, 3];

shellSort(arr); printArray(arr);

`

Complexity Analysis of Shell Sort:

**Time Complexity:

**Auxiliary Space: O(1), as it sorts in-place.
**Stability: Not stable (equal elements may not retain their original relative order).

Applications of Shell Sort:

Advantages

Disadvantages