Skip to main content

Shellsort

Definition​

Shellsort is an efficient sorting algorithm that improves upon the insertion sort algorithm by sorting sublists of the data and then eventually sorting the entire list. It belongs to the family of comparison-based sorting algorithms and operates by moving elements closer to their sorted position through a series of diminishing increment gaps

Practice​

shellSort(arr):
# Define increment sequence
gaps = [701, 301, 132, 57, 23, 10, 4, 1]

# Iterate over each gap
for gap in gaps:
# Perform insertion sort with current gap
for i = gap to length(arr):
temp = arr[i]
j = i
# Move elements of arr[0..i-gap] that are greater than temp to their positions ahead of current position
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j = j - gap
arr[j] = temp