Challenge - 5 Problems
Shell Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Shell Sort with a specific gap sequence
What is the output array after running the Shell Sort algorithm on the input array [23, 12, 1, 8, 34, 54, 2, 3] using the gap sequence [4, 2, 1]?
DSA C++
void shellSort(int arr[], int n) { int gaps[] = {4, 2, 1}; for (int gap : gaps) { 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; } } } int main() { int arr[] = {23, 12, 1, 8, 34, 54, 2, 3}; int n = sizeof(arr)/sizeof(arr[0]); shellSort(arr, n); for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Shell Sort sorts the array by comparing elements at a certain gap and reducing the gap gradually until it becomes 1.
✗ Incorrect
Shell Sort starts with a large gap and sorts elements far apart from each other. Then it reduces the gap and sorts again. Finally, it does a normal insertion sort with gap 1. This results in a sorted array.
🧠 Conceptual
intermediate1:30remaining
Understanding the role of gap sequence in Shell Sort
Why does Shell Sort use a gap sequence instead of just sorting adjacent elements like Insertion Sort?
Attempts:
2 left
💡 Hint
Think about how sorting far apart elements can help bring the array closer to sorted faster.
✗ Incorrect
Sorting elements far apart helps move elements closer to their final position quickly. This reduces the total work needed when the gap becomes 1, making the algorithm faster than simple insertion sort.
🔧 Debug
advanced2:00remaining
Identify the error in this Shell Sort implementation
What error will this Shell Sort code produce when run?
DSA C++
void shellSort(int arr[], int n) { int gap = n / 2; while (gap > 0) { 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; } gap /= 2; } }
Attempts:
2 left
💡 Hint
Check the comparison operator inside the inner while loop.
✗ Incorrect
The comparison uses '<' instead of '>'. This causes the algorithm to sort the array in descending order instead of ascending order.
📝 Syntax
advanced1:30remaining
Find the syntax error in this Shell Sort code snippet
Which option correctly fixes the syntax error in the following code?
for (int gap : gaps) {
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;
}
}
DSA C++
int gaps[] = {5, 3, 1}; int n = 10; int arr[10];
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
expert2:30remaining
Choosing the best gap sequence for Shell Sort
Which gap sequence is known to provide better average performance for Shell Sort compared to simple halving (n/2, n/4, ...)?
Attempts:
2 left
💡 Hint
Some gap sequences are experimentally proven to reduce the number of comparisons and swaps.
✗ Incorrect
The Ciura sequence is a well-known gap sequence that improves Shell Sort's efficiency by reducing the number of comparisons and swaps compared to simple halving.