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 Java code after performing Shell Sort on the array?
DSA Java
public class ShellSortTest { public static void main(String[] args) { int[] arr = {23, 12, 1, 8, 34, 54, 2, 3}; shellSort(arr); for (int num : arr) { System.out.print(num + " "); } } public static void shellSort(int[] arr) { int n = arr.length; 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
Shell Sort sorts the array by comparing elements at a certain gap and reducing the gap gradually.
✗ Incorrect
Shell Sort starts with a large gap and sorts elements that are far apart. Then it reduces the gap and sorts again until the gap is 1, which is a normal insertion sort. This results in a sorted array in ascending order.
🧠 Conceptual
intermediateGap sequence effect in Shell Sort
Which statement best describes the effect of the gap sequence on the Shell Sort algorithm's performance?
Attempts:
2 left
💡 Hint
Think about how elements move across the array during sorting.
✗ Incorrect
Starting with a larger gap allows elements to move farther in fewer steps, reducing the total number of comparisons and swaps compared to simple insertion sort.
🔧 Debug
advancedIdentify the error in Shell Sort implementation
What error will occur when running this Shell Sort code snippet?
DSA Java
public static void shellSort(int[] arr) { int n = arr.length; 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 the index used inside the while loop condition carefully.
✗ Incorrect
The condition uses arr[j + gap] which can go beyond array bounds. It should be arr[j - gap] to compare with the element gap positions before.
📝 Syntax
advancedSyntax error in Shell Sort code
Which option contains the correct syntax to declare and initialize the gap variable in Shell Sort?
Attempts:
2 left
💡 Hint
Look for the correct shorthand operator for division assignment.
✗ Incorrect
The operator '/=' divides the variable by the right-hand value and assigns the result back to the variable. Other options use longer syntax or have syntax errors or invalid operators.
🚀 Application
expertShell Sort on a large nearly sorted array
You have a large array that is almost sorted except for a few elements far from their correct position. Which property of Shell Sort makes it suitable for this scenario?
Attempts:
2 left
💡 Hint
Consider how Shell Sort handles elements that are far from their final position.
✗ Incorrect
Shell Sort's gap-based comparisons allow it to move elements that are far apart closer to their correct position quickly, making it efficient for nearly sorted arrays with some misplaced elements.
