Shell Sort Algorithm in DSA Python - 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.
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
This code sorts an array by comparing elements far apart and gradually reducing the gap.
Look at the loops and repeated steps:
- Primary operation: The inner while loop that shifts elements to insert the current element.
- How many times: This depends on the gap and the array size; the outer while loop reduces gap, and the for loop runs for each element beyond gap.
As the list size grows, the number of comparisons and shifts grows faster than the list size but slower than simple quadratic sorts.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20-30 operations |
| 100 | About 500-1000 operations |
| 1000 | About 10,000-20,000 operations |
Pattern observation: The operations grow faster than n but slower than n squared, roughly between n log n and n squared.
Time Complexity: O(n^{1.5})
This means the time grows faster than a simple linear scan but is much better than the slowest sorting methods.
[X] Wrong: "Shell Sort always runs in quadratic time like Bubble Sort."
[OK] Correct: Shell Sort uses gaps to reduce the number of comparisons early, so it usually runs faster than simple quadratic sorts.
Understanding Shell Sort's time complexity helps you explain how improving simple sorts can speed up sorting, a useful skill in interviews.
"What if we changed the gap sequence to use a different pattern, like 3x+1 increments? How would the time complexity change?"
Practice
Shell Sort algorithm?Solution
Step 1: Understand Shell Sort's approach
Shell Sort starts by sorting pairs of elements far apart from each other, then progressively reduces the gap between elements to be compared.Step 2: Compare with other sorting methods
Unlike bubble sort (adjacent swaps) or quicksort (pivot partition), Shell Sort uses a gap sequence to improve efficiency.Final Answer:
It sorts elements by comparing and swapping distant elements first, then reducing the gap. -> Option DQuick Check:
Shell Sort = Distant element comparisons first [OK]
- Confusing Shell Sort with bubble sort
- Thinking it always compares adjacent elements first
- Mixing it up with quicksort's pivot method
n?Solution
Step 1: Identify the initial gap value
Shell Sort typically starts with a gap of half the list length, i.e., gap = n // 2.Step 2: Understand gap reduction
The gap is then reduced by half each iteration until it reaches 0.Final Answer:
gap = n // 2 -> Option CQuick Check:
Initial gap = n // 2 [OK]
- Starting gap at 1 (too small)
- Starting gap equal to n (too large)
- Multiplying gap by 2 instead of dividing
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
arr = [9, 8, 3, 7, 5]
shell_sort(arr)
print(arr)Solution
Step 1: Trace the sorting process
The code implements Shell Sort correctly. It sorts the list [9, 8, 3, 7, 5] into ascending order.Step 2: Verify final sorted list
After sorting, the list becomes [3, 5, 7, 8, 9].Final Answer:
[3, 5, 7, 8, 9] -> Option BQuick Check:
Sorted list = [3, 5, 7, 8, 9] [OK]
- Assuming original list remains unchanged
- Confusing descending with ascending order
- Misreading the gap reduction loop
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 //= 2Solution
Step 1: Check the inner while loop condition
The condition uses arr[j + gap] which is incorrect; it should compare arr[j - gap] to temp.Step 2: Confirm correct element shifting
Using arr[j + gap] can cause index errors or wrong comparisons; arr[j - gap] ensures proper backward comparison.Final Answer:
The inner while condition uses arr[j + gap] instead of arr[j - gap]. -> Option AQuick Check:
Compare with arr[j - gap], not arr[j + gap] [OK]
- Using wrong index direction in comparisons
- Not reducing gap properly
- Incorrect loop start index
[23, 12, 1, 8, 34, 54, 2, 3], what will be the state of the list after the first gap iteration (gap = 4) in Shell Sort?Solution
Step 1: Identify elements compared with gap 4
Elements compared are pairs 0 & 4, 1 & 5, 2 & 6, 3 & 7.Step 2: Perform swaps if needed
Compare and swap if left element > right element:
Index 0 & 4: 23 > 34? No swap.
Index 1 & 5: 12 > 54? No swap.
Index 2 & 6: 1 > 2? No swap.
Index 3 & 7: 8 > 3? Yes, swap 8 and 3.Step 3: Result after first gap iteration
The list becomes [23, 12, 1, 3, 34, 54, 2, 8].Final Answer:
[23, 12, 1, 3, 34, 54, 2, 8] -> Option AQuick Check:
After gap=4: [23, 12, 1, 3, 34, 54, 2, 8] [OK]
- Not swapping elements correctly at gap distance
- Confusing final list after full sort with partial iteration
- Ignoring insertion steps within gap
Ready to prove it?
Pass the test and this pattern moves unknown
