Shell Sort Algorithm


Introduction to Shell Sort

Shell sort is an in-place comparison-based sorting algorithm. It generalizes insertion sort by allowing the exchange of items that are far apart. The algorithm starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. The efficiency of Shell sort depends on the gap sequence used, with time complexity varying from O(n^2) to O(n log n) based on the sequence.


Step-by-Step Process

Consider an array with the following elements:


[12, 34, 54, 2, 3]

To sort this array using Shell sort, follow these steps:

  1. Choose a Gap Sequence: Choose a gap sequence to determine the intervals for comparing elements. A common sequence is to start with n/2 and halve the gap until it reaches 1.
  2. Sort Elements with Current Gap: For each gap, perform a gapped insertion sort on the array. Compare and swap elements that are 'gap' distance apart.
  3. Reduce Gap and Repeat: Reduce the gap and repeat the process until the gap is 1, at which point the array is fully sorted using a standard insertion sort.

Let's apply these steps to the example array:

  • Initial array: [12, 34, 54, 2, 3]
  • Gap = 2: Compare and swap elements that are 2 positions apart
  • Array after gap 2: [12, 3, 54, 2, 34]
  • Gap = 1: Perform a standard insertion sort
  • Final sorted array: [2, 3, 12, 34, 54]

Pseudo Code for Shell Sort

Below is the pseudo code for the Shell sort algorithm:


function shellSort(array)
    n = length(array)
    gap = n // 2
    while gap > 0 do
        for i = gap to n - 1 do
            temp = array[i]
            j = i
            while j >= gap and array[j - gap] > temp do
                array[j] = array[j - gap]
                j = j - gap
            end while
            array[j] = temp
        end for
        gap = gap // 2
    end while
end function

Explanation:

  • Initialize: The shellSort function starts by determining the initial gap, usually set to half the length of the array.
  • Gapped Insertion Sort: For each gap, the algorithm performs a gapped insertion sort, where elements that are 'gap' distance apart are compared and swapped if necessary.
  • Reduce Gap: The gap is reduced by half after each iteration, and the process is repeated until the gap is 1, at which point a final insertion sort is performed to fully sort the array.

Python Program to Implement Shell Sort

def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= 1
            arr[j] = temp
        gap //= 2
    return arr

# Example usage:
array = [12, 34, 54, 2, 3]
print("Original array:", array)
sorted_array = shell_sort(array)
print("Sorted array:", sorted_array)  # Output: [2, 3, 12, 34, 54]

This Python program defines a function to perform Shell sort on an array. The function initializes the gap, performs a gapped insertion sort for each gap, and reduces the gap until the array is fully sorted.