š Scenario: You are working on a program that needs to sort a list of numbers quickly. Sorting helps organize data so you can find things faster, like arranging books by their titles on a shelf.
šÆ Goal: Build a Rust program that sorts a list of numbers using the Quick Sort algorithm. You will create the list, set up a helper function, implement the sorting logic, and then print the sorted list.
š What You'll Learn
Create a vector called numbers with the exact values: 29, 10, 14, 37, 13
Create a function called quick_sort that takes a mutable slice of i32
Implement the Quick Sort logic inside the quick_sort function using recursion and partitioning
Call the quick_sort function on the numbers vector
Print the sorted numbers vector
š” Why This Matters
š Real World
Sorting is used everywhere, like organizing files, searching data quickly, or arranging products by price in online stores.
š¼ Career
Understanding sorting algorithms like Quick Sort is essential for software developers to write efficient code and optimize performance.
Progress0 / 4 steps
1
Create the initial list of numbers
Create a vector called numbers with these exact values: 29, 10, 14, 37, 13
DSA Rust
Hint
Use let mut numbers = vec![29, 10, 14, 37, 13]; to create a mutable vector.
2
Create the Quick Sort function signature
Create a function called quick_sort that takes a mutable slice of i32 named arr
DSA Rust
Hint
Define the function with fn quick_sort(arr: &mut [i32]).
3
Implement the Quick Sort logic
Inside the quick_sort function, implement the Quick Sort algorithm using recursion and partitioning. Use if arr.len() <= 1 { return; } to stop recursion. Partition the array around a pivot and recursively sort the left and right parts.
DSA Rust
Hint
Use a helper function partition to rearrange elements and return the pivot index. Then recursively call quick_sort on the left and right parts.
4
Call quick_sort and print the sorted list
Call the quick_sort function on the numbers vector inside main. Then print the sorted numbers vector using println!
DSA Rust
Hint
Call quick_sort(&mut numbers); and then print with println!("{:?}", numbers);.
Practice
(1/5)
1. What is the main idea behind the Quick Sort algorithm?
easy
A. Merge two sorted lists into one sorted list
B. Compare every element with every other element and swap if needed
C. Sort the list by repeatedly finding the minimum element
D. Divide the list into smaller parts using a pivot and sort them recursively
Solution
Step 1: Understand Quick Sort's approach
Quick Sort picks a pivot element and divides the list into parts smaller and larger than the pivot.
Step 2: Recursive sorting of parts
It then sorts these parts recursively, combining them to get the sorted list.
Final Answer:
Divide the list into smaller parts using a pivot and sort them recursively -> Option D
Quick Check:
Quick Sort = Divide and conquer [OK]
Hint: Remember: pivot divides list, then sort parts recursively [OK]
Common Mistakes:
Confusing Quick Sort with Bubble Sort
Thinking Quick Sort merges lists like Merge Sort
Believing Quick Sort always picks the first element as pivot
2. Which of the following is the correct Rust function signature for a Quick Sort function that sorts a mutable slice of integers?
easy
A. fn quick_sort(arr: &mut [i32])
B. fn quick_sort(arr: Vec<i32>) -> Vec<i32>
C. fn quick_sort(arr: &[i32]) -> &[i32]
D. fn quick_sort(arr: &mut Vec<i32>) -> ()
Solution
Step 1: Identify correct parameter type for in-place sorting
Quick Sort usually sorts in place, so it needs a mutable slice reference: &mut [i32].
Step 2: Check function signature correctness
fn quick_sort(arr: &mut [i32]) matches this: it takes &mut [i32] and returns nothing (unit type implied).
Hint: Quick Sort sorts in place, so use &mut slice parameter [OK]
Common Mistakes:
Using immutable slice &[] which cannot be changed
Returning a new vector instead of sorting in place
Using Vec<i32> without mutability for in-place sort
3. What will be the output of the following Rust code snippet after calling quick_sort(&mut arr)?
fn quick_sort(arr: &mut [i32]) {
if arr.len() <= 1 { return; }
let pivot = arr[0];
let mut left = 1;
let mut right = arr.len() - 1;
while left <= right {
if arr[left] <= pivot {
left += 1;
} else {
arr.swap(left, right);
if right == 0 { break; }
right -= 1;
}
}
arr.swap(0, right);
quick_sort(&mut arr[0..right]);
quick_sort(&mut arr[right+1..]);
}
fn main() {
let mut arr = [4, 2, 7, 1, 3];
quick_sort(&mut arr);
println!("{:?}", arr);
}
medium
A. [1, 2, 3, 7, 4]
B. [1, 2, 3, 4, 7]
C. [4, 2, 7, 1, 3]
D. [7, 4, 3, 2, 1]
Solution
Step 1: Understand partitioning around pivot
The pivot is 4. Elements <=4 move left, >4 move right. After partition, array becomes [3, 2, 1, 4, 7] with pivot at index 3.
Step 2: Recursive calls sort left and right parts
Left part [3, 2, 1] sorts to [1, 2, 3], right part [7] is already sorted. Combined final array is [1, 2, 3, 4, 7].
Final Answer:
[1, 2, 3, 4, 7] -> Option B
Quick Check:
Sorted array = [1, 2, 3, 4, 7] [OK]
Hint: Trace pivot swaps and recursive calls carefully [OK]
Common Mistakes:
Ignoring pivot swaps and recursive sorting
Assuming array remains unchanged
Mixing up left and right partitions
4. Identify the bug in this Rust Quick Sort implementation:
fn quick_sort(arr: &mut [i32]) {
if arr.len() <= 1 { return; }
let pivot = arr[0];
let mut left = 1;
let mut right = arr.len() - 1;
while left < right {
if arr[left] <= pivot {
left += 1;
} else {
arr.swap(left, right);
right -= 1;
}
}
arr.swap(0, right);
quick_sort(&mut arr[0..right]);
quick_sort(&mut arr[right+1..]);
}
medium
A. The while loop condition should be <= instead of <
B. Pivot should be chosen as the last element, not the first
C. The recursive calls should use inclusive ranges
D. The swap after the loop should be with left, not right
Solution
Step 1: Analyze the while loop condition
The loop uses while left < right, but it must include the case when left == right to correctly partition.
Step 2: Effect of incorrect loop condition
Stopping early misses some comparisons, causing incorrect partition and sorting errors.
Final Answer:
The while loop condition should be <= instead of < -> Option A
Quick Check:
Loop condition must cover all indices [OK]
Hint: Check loop boundaries carefully in partition step [OK]
Common Mistakes:
Ignoring edge case when left equals right
Changing pivot choice unnecessarily
Misusing slice ranges in recursive calls
5. You want to sort a large list of integers using Quick Sort in Rust. Which strategy helps avoid worst-case performance when the input is already sorted or nearly sorted?
hard
A. Always pick the first element as pivot
B. Use Bubble Sort instead for sorted inputs
C. Pick a random element as pivot each time
D. Sort only the first half of the list
Solution
Step 1: Understand worst-case in Quick Sort
Picking the first element as pivot on sorted input causes unbalanced partitions, leading to worst-case O(n²) time.
Step 2: Use random pivot to avoid worst-case
Choosing a random pivot spreads partitions more evenly, improving average performance and avoiding worst-case.
Final Answer:
Pick a random element as pivot each time -> Option C
Quick Check:
Random pivot = better average case [OK]
Hint: Randomize pivot to prevent worst-case input issues [OK]
Common Mistakes:
Always picking first element as pivot
Switching to Bubble Sort unnecessarily
Sorting only part of the list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.