Skip to main content

Shell Sort (Geeks for Geeks)

1. What is Shell Sort?​

Shell Sort is an in-place comparison-based sorting algorithm. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared.

2. Algorithm for Shell Sort​

  1. Start with a large gap between elements and reduce the gap until there is no gap.
  2. Perform a gapped insertion sort for each gap size. The algorithm uses insertion sort to sort elements that are a certain gap distance apart.
  3. Reduce the gap and repeat the process until the gap becomes zero.

3. How does Shell Sort work?​

  • Shell Sort works by comparing elements that are far apart, then progressively reducing the gap between elements.
  • This allows elements to move quickly towards their correct position.

4. Problem Description​

Given an array of integers, implement the Shell Sort algorithm to sort the array in ascending order.

5. Examples​

Example 1:

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

Example 2:

Input: [37, 45, 29, 8, 12, 88, -3]
Output: [-3, 8, 12, 29, 37, 45, 88]

Explanation of Example 1:

  • The initial array is [12, 34, 54, 2, 3].
  • With a gap of 2, the array becomes [12, 34, 2, 3, 54].
  • With a gap of 1, the array becomes [2, 3, 12, 34, 54].

6. Constraints​

  • Thearraycanhaveanynumberofelements.The array can have any number of elements.
  • Allelementsinthearrayareintegers.All elements in the array are integers.

7. Implementation​

Written by @ngmuraqrdd
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 -= gap
arr[j] = temp
gap //= 2
return arr

# Example usage:
arr = [12, 34, 54, 2, 3]
shell_sort(arr)
print(arr)

8. Complexity Analysis​

  • Time Complexity:

    • Best case: O(nlogn)O(n log n)
    • Average case: O(n(3/2))O(n^(3/2))
    • Worst case: O(n2)O(n^2)
  • Space Complexity: O(1)O(1) (in-place sorting)

9. Advantages and Disadvantages​

Advantages:

  • More efficient than bubble sort and insertion sort for large lists.
  • Simple to understand and implement.

Disadvantages:

  • More complex than bubble sort and insertion sort.
  • The performance depends on the gap sequence used.

10. References​