Challenge - 5 Problems
Shell Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Shell Sort with gap sequence
What is the output of the following Shell Sort code on the list [23, 12, 1, 8, 34, 54, 2, 3]?
DSA Python
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 = [23, 12, 1, 8, 34, 54, 2, 3] shell_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
Trace the sorting steps with gap reducing by half each time.
✗ Incorrect
Shell Sort sorts elements by comparing elements at a certain gap and reducing the gap until it becomes 1, which is a normal insertion sort. The final sorted list is ascending.
🧠 Conceptual
intermediateUnderstanding gap sequence in Shell Sort
Which of the following statements about the gap sequence in Shell Sort is TRUE?
Attempts:
2 left
💡 Hint
Think about how the gap affects sorting passes.
✗ Incorrect
The gap sequence affects how quickly the list becomes sorted. It must end with 1 to ensure the list is fully sorted. Different sequences affect performance.
🔧 Debug
advancedIdentify the error in this Shell Sort implementation
What error will this Shell Sort code produce when run?
DSA Python
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 = [5, 3, 8, 6, 2] shell_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
Check the comparison operator in the inner while loop.
✗ Incorrect
The comparison uses '<' instead of '>' causing the list to be sorted in descending order.
📝 Syntax
advancedSyntax error in Shell Sort code
Which option contains the correct syntax to fix the error in this Shell Sort snippet?
Code snippet:
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
Attempts:
2 left
💡 Hint
Check Python syntax for loops.
✗ Incorrect
Python requires a colon at the end of for loop declarations.
🚀 Application
expertShell Sort performance on nearly sorted list
Given a nearly sorted list of 1000 elements, which statement about Shell Sort's performance is MOST accurate?
Attempts:
2 left
💡 Hint
Consider how gap insertion sort benefits nearly sorted data.
✗ Incorrect
Shell Sort improves over simple insertion sort by reducing swaps early, making it efficient on nearly sorted data.
