Shell Sort Algorithm in DSA Go - Time & Space Complexity
We want to understand how the time taken by Shell Sort changes as the list size grows.
How does the number of steps increase when sorting bigger lists?
Analyze the time complexity of the following Shell Sort code.
func shellSort(arr []int) {
n := len(arr)
for gap := n / 2; gap > 0; gap /= 2 {
for i := gap; i < n; i++ {
temp := arr[i]
j := i
for j >= gap && arr[j-gap] > temp {
arr[j] = arr[j-gap]
j -= gap
}
arr[j] = temp
}
}
}
This code sorts an array by comparing elements far apart and gradually reducing the gap.
Look at the loops that repeat work:
- Primary operation: The inner loop that shifts elements by the current gap.
- How many times: The outer loop reduces gap roughly by half each time, and the inner loops run over the array for each gap.
As the list size grows, the number of comparisons and shifts depends on the gap sequence.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20-30 operations |
| 100 | Few thousands of operations |
| 1000 | Hundreds of thousands of operations |
Pattern observation: The operations grow faster than linear but slower than quadratic, depending on gap choices.
Time Complexity: O(n^{1.5})
This means the time grows faster than a simple list scan but much slower than checking every pair.
[X] Wrong: "Shell Sort always runs in quadratic time like Bubble Sort."
[OK] Correct: Shell Sort uses gaps to reduce comparisons, so it usually runs faster than simple quadratic sorts.
Understanding Shell Sort's time complexity shows you how clever ideas can improve sorting speed beyond simple methods.
"What if we changed the gap sequence to use a different pattern? How would the time complexity change?"
Practice
Shell Sort Algorithm?Solution
Step 1: Understand Shell Sort concept
Shell Sort improves insertion sort by comparing elements far apart, reducing the gap over iterations.Step 2: Compare with other sorting methods
Unlike bubble or insertion sort, Shell Sort starts with distant comparisons, not just adjacent swaps.Final Answer:
It sorts elements by comparing distant elements and gradually reducing the gap. -> Option CQuick Check:
Shell Sort = distant comparisons + gap reduction [OK]
- Thinking Shell Sort only swaps adjacent elements
- Confusing Shell Sort with merge or quick sort
- Assuming it inserts elements one by one without gaps
Solution
Step 1: Identify gap initialization in Shell Sort
The gap usually starts as half the array length to compare distant elements first.Step 2: Check Go syntax for variable initialization
Usinggap := len(arr) / 2correctly declares and initializes gap in Go.Final Answer:
gap := len(arr) / 2 -> Option BQuick Check:
gap = half array length [OK]
- Using gap larger than array length
- Initializing gap to zero which stops sorting
- Incorrect Go syntax for variable declaration
[9, 8, 3, 7, 5]?
func shellSort(arr []int) []int {
n := len(arr)
for gap := n / 2; gap > 0; gap /= 2 {
for i := gap; i < n; i++ {
temp := arr[i]
j := i
for j >= gap && arr[j-gap] > temp {
arr[j] = arr[j-gap]
j -= gap
}
arr[j] = temp
}
}
return arr
}
fmt.Println(shellSort([]int{9, 8, 3, 7, 5}))Solution
Step 1: Trace first gap iteration (gap=2)
Compare elements 2 apart and sort: array changes from [9,8,3,7,5] to [3,8,9,7,5] then [3,5,9,7,8].Step 2: Trace second gap iteration (gap=1)
Standard insertion sort on nearly sorted array results in [3,5,7,8,9].Final Answer:
[3 5 7 8 9] -> Option AQuick Check:
Shell Sort output = sorted array [OK]
- Confusing gap steps and final output
- Assuming no change after first gap
- Mixing up array indices during swaps
func shellSort(arr []int) {
n := len(arr)
gap := n / 2
for gap > 0 {
for i := gap; i < n; i++ {
temp := arr[i]
j := i
for j >= gap && arr[j-gap] < temp {
arr[j] = arr[j-gap]
j -= gap
}
arr[j] = temp
}
gap /= 2
}
}Solution
Step 1: Analyze inner loop comparison
The inner loop compares arr[j-gap] and temp to decide swapping. It should move larger elements up, so condition must be arr[j-gap] > temp.Step 2: Check current comparison operator
Current code uses arr[j-gap] < temp which incorrectly moves smaller elements, breaking sort order.Final Answer:
The comparison operator should be > instead of < in inner loop. -> Option DQuick Check:
Correct comparison = > [OK]
- Using < instead of > in inner loop condition
- Increasing gap instead of decreasing
- Misplacing gap update outside loop
Solution
Step 1: Understand stability in sorting
Stable sorting means equal elements keep their original order after sorting.Step 2: Adapt Shell Sort for stability
Using a stable gap sequence like Hibbard's and avoiding swaps when elements are equal helps maintain stability.Step 3: Evaluate other options
Using >= breaks stability, increasing gap skips elements, and claiming impossibility is incorrect.Final Answer:
Use a stable gap sequence like Hibbard's sequence and avoid swapping equal elements. -> Option AQuick Check:
Stable Shell Sort = stable gaps + no equal swaps [OK]
- Using >= comparison breaking stability
- Assuming Shell Sort cannot be stable
- Increasing gap size to skip duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
