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 C# code after performing Shell Sort on the array?
DSA C#
using System; class Program { 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; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { arr[j] = arr[j - gap]; } arr[j] = temp; } } } static void Main() { int[] data = {23, 12, 1, 8, 34, 54, 2, 3}; ShellSort(data); foreach (int num in data) { Console.Write(num + " "); } } }
Attempts:
2 left
💡 Hint
Shell Sort sorts the array in ascending order by comparing elements at a gap and reducing the gap gradually.
✗ Incorrect
Shell Sort starts with a large gap and reduces it, sorting elements that are far apart first. The final output is a sorted array in ascending order.
🧠 Conceptual
intermediateUnderstanding the gap sequence in Shell Sort
In Shell Sort, what is the purpose of the 'gap' variable during the sorting process?
Attempts:
2 left
💡 Hint
Think about how Shell Sort compares elements that are not next to each other initially.
✗ Incorrect
The gap controls how far apart elements are compared. Starting with a large gap helps move elements closer to their correct position faster, improving efficiency.
🔧 Debug
advancedIdentify the error in this Shell Sort implementation
What error will this C# Shell Sort code produce when run?
DSA C#
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
Look carefully at the comparison operator in the while loop.
✗ Incorrect
The comparison uses '<' instead of '>' causing the sort to arrange elements in descending order instead of ascending.
❓ Predict Output
advancedShell Sort output with negative and positive numbers
What is the output of this C# Shell Sort code after sorting the array?
DSA C#
using System; class Program { 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; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { arr[j] = arr[j - gap]; } arr[j] = temp; } } } static void Main() { int[] data = {0, -10, 5, -3, 8, 2, -1}; ShellSort(data); foreach (int num in data) { Console.Write(num + " "); } } }
Attempts:
2 left
💡 Hint
Shell Sort sorts the array in ascending order regardless of negative or positive values.
✗ Incorrect
The algorithm correctly sorts negative and positive numbers in ascending order.
🧠 Conceptual
expertTime complexity of Shell Sort depends on gap sequence
Which statement about the time complexity of Shell Sort is correct?
Attempts:
2 left
💡 Hint
The choice of gap sequence affects how efficiently the array is sorted.
✗ Incorrect
Using optimized gap sequences like Ciura's can improve Shell Sort's average and worst-case time complexity closer to O(n log n).
