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 halving
What is the output of the following Rust code that performs Shell Sort on the array?
DSA Rust
fn shell_sort(arr: &mut [i32]) {
let n = arr.len();
let mut gap = n / 2;
while gap > 0 {
for i in gap..n {
let temp = arr[i];
let mut j = i;
while j >= gap && arr[j - gap] > temp {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap /= 2;
}
}
fn main() {
let mut data = [23, 12, 1, 8, 34, 54, 2, 3];
shell_sort(&mut data);
println!("{:?}", data);
}Attempts:
2 left
💡 Hint
Think about how Shell Sort reduces the gap and sorts subarrays.
✗ Incorrect
Shell Sort starts with a large gap and reduces it, sorting elements far apart first. This leads to a fully sorted array at the end.
🧠 Conceptual
intermediateUnderstanding the gap sequence in Shell Sort
In Shell Sort, what is the main purpose of using a gap sequence that decreases over time?
Attempts:
2 left
💡 Hint
Think about how sorting distant elements first helps overall sorting.
✗ Incorrect
The gap sequence allows Shell Sort to move elements closer to their correct position faster by comparing elements far apart, which reduces disorder efficiently before finishing with smaller gaps.
🔧 Debug
advancedIdentify the error in this Shell Sort implementation
What error will this Rust Shell Sort code produce when run?
DSA Rust
fn shell_sort(arr: &mut [i32]) {
let n = arr.len();
let mut gap = n / 2;
while gap > 0 {
for i in gap..n {
let temp = arr[i];
let mut j = i;
while j >= gap && arr[j - gap] > temp {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap /= 0;
}
}
fn main() {
let mut data = [5, 3, 8, 6, 2];
shell_sort(&mut data);
println!("{:?}", data);
}Attempts:
2 left
💡 Hint
Look carefully at how gap is updated inside the loop.
✗ Incorrect
The line gap /= 0 causes a division by zero panic at runtime, which crashes the program.
📝 Syntax
advancedSyntax error in Shell Sort Rust code
Which option contains the correct syntax to declare and initialize the gap variable for Shell Sort in Rust?
Attempts:
2 left
💡 Hint
Remember Rust variable declaration syntax.
✗ Incorrect
In Rust, variables are declared with let, optionally mutable with mut, and use = for assignment. Other options use syntax from other languages or are incomplete.
🚀 Application
expertShell Sort performance on nearly sorted array
Given a nearly sorted array of 1,000 elements, which statement best describes Shell Sort's performance compared to simple insertion sort?
Attempts:
2 left
💡 Hint
Think about how Shell Sort handles distant elements compared to insertion sort.
✗ Incorrect
Shell Sort improves on insertion sort by moving elements over large gaps early, which reduces the total number of shifts even in nearly sorted arrays, often leading to better performance.
