Challenge - 5 Problems
Quick Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Quick Sort Partition Step
What is the output of the partition function on the array after the first partition call?
DSA Rust
fn partition(arr: &mut [i32], low: usize, high: usize) -> usize {
let pivot = arr[high];
let mut i = low;
for j in low..high {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, high);
i
}
fn main() {
let mut arr = [10, 7, 8, 9, 1, 5];
let p = partition(&mut arr, 0, arr.len() - 1);
println!("{:?}", arr);
println!("Partition index: {}", p);
}Attempts:
2 left
š” Hint
Remember the pivot is the last element and elements less than pivot are moved to the left.
ā Incorrect
The partition function uses the last element as pivot (5). It moves all elements less than 5 to the left. After partition, the array becomes [1, 5, 8, 9, 7, 10] and pivot index is 1.
ā Predict Output
intermediateFinal Sorted Array from Quick Sort
What is the output of the array after the full quick sort completes?
DSA Rust
fn quick_sort(arr: &mut [i32], low: isize, high: isize) {
if low < high {
let p = partition(arr, low as usize, high as usize);
quick_sort(arr, low, p as isize - 1);
quick_sort(arr, p as isize + 1, high);
}
}
fn partition(arr: &mut [i32], low: usize, high: usize) -> usize {
let pivot = arr[high];
let mut i = low;
for j in low..high {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, high);
i
}
fn main() {
let mut arr = [10, 7, 8, 9, 1, 5];
let len = arr.len();
quick_sort(&mut arr, 0, (len - 1) as isize);
println!("{:?}", arr);
}Attempts:
2 left
š” Hint
Quick sort sorts the array in ascending order.
ā Incorrect
Quick sort recursively partitions and sorts the array. The final sorted array is [1, 5, 7, 8, 9, 10].
š§ Debug
advancedIdentify the Bug in Quick Sort Recursive Call
What error will this quick sort code produce when run?
DSA Rust
fn quick_sort(arr: &mut [i32], low: usize, high: usize) {
if low < high {
let p = partition(arr, low, high);
quick_sort(arr, low, p - 1);
quick_sort(arr, p + 1, high);
}
}
fn partition(arr: &mut [i32], low: usize, high: usize) -> usize {
let pivot = arr[high];
let mut i = low;
for j in low..high {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, high);
i
}
fn main() {
let mut arr = [3, 6, 8, 10, 1, 2, 1];
quick_sort(&mut arr, 0, arr.len() - 1);
println!("{:?}", arr);
}Attempts:
2 left
š” Hint
Check what happens when p equals low (0) and you subtract 1 from it.
ā Incorrect
Since low and high are usize (unsigned), p - 1 when p is 0 causes underflow panic at runtime.
š§ Conceptual
advancedTime Complexity of Quick Sort in Worst Case
What is the worst-case time complexity of quick sort and when does it occur?
Attempts:
2 left
š” Hint
Think about how unbalanced partitions affect recursion depth.
ā Incorrect
Worst case O(n^2) happens when pivot is smallest or largest element repeatedly, causing unbalanced partitions.
š Application
expertQuick Sort Partition Index for Duplicate Elements
Given the array [4, 5, 4, 3, 4, 2, 4] and using the last element as pivot, what is the partition index after the first partition call?
DSA Rust
fn partition(arr: &mut [i32], low: usize, high: usize) -> usize {
let pivot = arr[high];
let mut i = low;
for j in low..high {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, high);
i
}
fn main() {
let mut arr = [4, 5, 4, 3, 4, 2, 4];
let p = partition(&mut arr, 0, arr.len() - 1);
println!("Partition index: {}", p);
println!("Array after partition: {:?}", arr);
}Attempts:
2 left
š” Hint
Elements equal to pivot are not moved before pivot index.
ā Incorrect
Only elements less than pivot (4) move left. Elements equal to pivot stay after. Partition index is 4 after swaps.
