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 on a small array
What is the output of the following Shell Sort implementation on the array [9, 8, 3, 7, 5]?
DSA C
void shellSort(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int temp = arr[i]; int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { arr[j] = arr[j - gap]; } arr[j] = temp; } } } int main() { int arr[] = {9, 8, 3, 7, 5}; int n = 5; shellSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Shell Sort sorts the array by comparing elements far apart and gradually reducing the gap.
✗ Incorrect
Shell Sort starts with a gap of n/2 and reduces it by half each iteration. It sorts elements at each gap using insertion sort logic. The final output is a sorted array in ascending order.
🧠 Conceptual
intermediateUnderstanding the 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 reduces to eventually perform a final insertion sort.
✗ Incorrect
Shell Sort begins with a gap of half the array size and reduces it by half each iteration until the gap is 1, where it performs a final insertion sort pass.
🔧 Debug
advancedIdentify the error in this Shell Sort code snippet
What error will occur when running this Shell Sort code snippet?
DSA C
void shellSort(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int temp = arr[i]; int j = i; while (j >= gap && arr[j - gap] < temp) { arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; } } }
Attempts:
2 left
💡 Hint
Look at the comparison operator in the while loop condition.
✗ Incorrect
The comparison uses '<' which causes the array to be sorted in descending order instead of ascending order.
📝 Syntax
advancedFind the syntax error in this Shell Sort implementation
Which option correctly fixes the syntax error in the following code snippet?
DSA C
void shellSort(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int temp = arr[i] int j = i; while (j >= gap && arr[j - gap] > temp) { arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; } } }
Attempts:
2 left
💡 Hint
Check for missing punctuation after variable declarations.
✗ Incorrect
The line 'int temp = arr[i]' is missing a semicolon at the end, causing a syntax error.
🚀 Application
expertShell Sort gap sequence effect on performance
Which gap sequence is known to provide better average-case performance for Shell Sort compared to the simple n/2 halving sequence?
Attempts:
2 left
💡 Hint
Some gap sequences reduce the number of comparisons and swaps more efficiently.
✗ Incorrect
The 3x + 1 gap sequence (also called Knuth's sequence) is known to improve Shell Sort's average performance by reducing the number of comparisons and swaps.
