Shell Sort
Shell Sort
What is Shell
Sort?
Definition:
• Shell Sort is an advanced sorting algorithm that generalizes
Insertion Sort to allow the exchange of items that are far
apart.
• It is named after its inventor, Donald Shell, who introduced it
in 1959.
Concept:
• It starts by sorting elements that are far apart, using a gap
sequence.
• It gradually reduces the gap between elements to be Insetion
compared until it performs a final insertion sort with a gap of sort
1.
Purpose:
• Shell Sort is designed to improve the efficiency of insertion
sort for larger lists.
• It breaks down the list into smaller sublists that are sorted
individually. Shell
sort
Step-by-Step
Algorithm
1.Initialize
⚬ Set a gap sequence based on the length of
the array (commonly gap = n/2).
2.While Gap > 0
⚬ For each element from the gap index to the
end of the array, compare it with elements
at intervals of the gap.
3.Sort within Gaps
⚬ Use a modified insertion sort that swaps
elements if they are out of order,
considering the gap distance.
4.Reduce Gap
⚬ Update the gap sequence (e.g., gap =
gap / 2).
5.Repeat
⚬ Continue until the gap is reduced to 1 and
the array is sorted.
Approach & Pseudo
Code
• Initial Gap Selection: ShellSort(array, n)
// Start with a large gap, then reduce the gap
⚬ Start with a large gap, usually set to half the gap = n / 2
length of the array (gap = n/2).
⚬ The gap is then progressively reduced in // Continue until the gap becomes zero
while gap > 0
each subsequent pass (e.g., gap = gap/2).
// Perform gapped insertion sort for this gap size
// The first gap elements array[0..gap-1] are already in gapped
• Sorting within Gaps: order
⚬ For each gap, use Insertion Sort to compare for i = gap to n - 1
// Save array[i] in temp and make a hole at position i
and sort elements that are gap positions temp = array[i]
apart.
⚬ This step ensures that elements move closer // Shift earlier gap-sorted elements up until the correct
location
to their final sorted positions.
// for array[i] is found
j=i
• Final Pass: while j >= gap and array[j - gap] > temp
⚬ When the gap reduces to 1, a final pass of array[j] = array[j - gap]
j = j - gap
Insertion Sort is applied on the entire array.
⚬ By this stage, most elements are already // Put temp (the original array[i]) in its correct location
sorted, making the final pass quick and array[j] = temp
efficient.
// Reduce the gap for the next iteration
Example
>Suppose we have the following array of Step 3: Reduce the gap and
integers: [5, 2, 8, 3, 1, 6, 4] repeat
We will use Shell sort to sort this array. New gap (h) = 2
Step 1: Choose the gap (h) Compare elements 2 positions
Initial gap (h) = 4 (half of the array apart:
size). [1, 2, 4, 3, 5, 6, 8]
Step 2: Perform insertion sort for Compare 4 and 1, no swap:
elements separated by h. Compare 3 and 2, no swap:
Compare elements 4 positions apart: [1, 2, 4, 3, 5, 6, 8]
[5, 2, 8, 3, 1, 6, 4] Compare 5 and 4, no swap:
Compare 5 and 1, swap: Compare 6 and 5, no swap:
[1, 2, 8, 3, 5, 6, 4] Compare 8 and 6, no swap:
Compare 8 and 4, swap: Step 4: Final insertion sort
[1, 2, 4, 3, 5, 6, 8] New gap (h) = 1
Perform standard insertion sort:
[1, 2, 3, 4, 5, 6, 8]
Sorted array:
Time
Cimplexity
• Best Case: O(n log n)
⚬ When the array is already partially sorted or the chosen
gap sequence is optimal, Shell Sort can achieve near-
linear performance.
Comparison Compares and swaps adjacent Compares and swaps elements that are far apart
Scope elements only. using a gap sequence.
Less efficient for large datasets due More efficient for larger datasets as elements
Efficiency to high number of shifts and are moved closer to their sorted position in
comparisons. earlier passes.
Advantages: Limitations:
• Faster than Insertion Sort for larger • Performance depends heavily on the chosen
arrays. gap sequence.
• Simple to implement and understand. • Not a stable sorting algorithm (may not
• Efficient for medium-sized arrays and preserve the order of equal elements).
partially sorted arrays. • For very large arrays, other sorting
• In-place sorting algorithm with a space algorithms like Quick Sort or Merge Sort are
complexity of O(1). more efficient.
• Reduces the number of shifts compared • Difficult to determine the optimal gap
to Insertion Sort by using gap-based sequence for the best performance in all
sorting. cases.
BACK TO AGENDA
PAGE
Thanks