![]() |
VOOZH | about |
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.
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):
Reduce gap: gap = 1 (regular insertion sort step)
Pass 2 (gap = 1):
[1, 3, 5, 9] (sorted).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.
[2, 3, 12, 34, 54]
Explanation:
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.
[2, 3, 12, 34, 54]
Explanation:
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.
[2, 3, 12, 34, 54]
Explanation:
Please refer complete article on ShellSort for more details!