Comparison Based vs Non Comparison Based Sorting in DSA Rust - Complexity Comparison
Sorting is a common task where we arrange items in order. Different sorting methods take different times depending on how they work.
We want to understand how the time to sort grows as the list gets bigger.
Analyze the time complexity of these two sorting approaches.
// Comparison based sorting example: Bubble Sort
fn bubble_sort(arr: &mut [i32]) {
let n = arr.len();
for i in 0..n {
for j in 0..n - i - 1 {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
}
// Non-comparison based sorting example: Counting Sort
fn counting_sort(arr: &mut [usize], max_val: usize) {
let mut count = vec![0; max_val + 1];
for &num in arr.iter() {
count[num] += 1;
}
let mut idx = 0;
for num in 0..=max_val {
for _ in 0..count[num] {
arr[idx] = num;
idx += 1;
}
}
}
Bubble Sort compares and swaps elements repeatedly. Counting Sort counts occurrences and places elements directly.
Look at the loops and repeated steps in each method.
- Bubble Sort primary operation: Comparing and swapping pairs inside nested loops.
- Bubble Sort how many times: Outer loop runs n times, inner loop runs up to n - i - 1 times, so about n x n = n² times.
- Counting Sort primary operation: Counting each number once, then placing numbers back.
- Counting Sort how many times: Counting loop runs n times, placement loop runs max_val times (range size).
As the list size grows, how does the work increase?
| Input Size (n) | Bubble Sort Ops (approx.) | Counting Sort Ops (approx.) |
|---|---|---|
| 10 | 100 (10x10) | 10 + max_val |
| 100 | 10,000 (100x100) | 100 + max_val |
| 1000 | 1,000,000 (1000x1000) | 1000 + max_val |
Bubble Sort grows very fast as n increases (squares). Counting Sort grows mostly with n plus the range size, which can be smaller.
Time Complexity: O(n²) for comparison based (Bubble Sort), O(n + k) for non comparison based (Counting Sort), where k is the range of input values.
This means Bubble Sort gets much slower as the list grows, but Counting Sort can be faster if the range k is not too big.
[X] Wrong: "All sorting methods take about the same time because they all sort the list."
[OK] Correct: Different methods use different steps. Some compare many times, others count and place directly. This changes how time grows with list size.
Understanding these differences helps you choose the right sorting method and explain your choice clearly. It shows you know how algorithms behave as data grows.
"What if the input range k in Counting Sort is much larger than n? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand comparison based sorting
Comparison based sorting algorithms decide order by comparing elements, like Merge Sort, Quick Sort, and Heap Sort.Step 2: Identify non-comparison based sorting
Counting Sort uses element values directly to sort without comparing elements.Final Answer:
Counting Sort -> Option DQuick Check:
Non-comparison based = Counting Sort [OK]
- Confusing Merge Sort as non-comparison
- Thinking Quick Sort is non-comparison
- Assuming Heap Sort is non-comparison
counting_sort on a mutable vector arr?Solution
Step 1: Understand Rust borrowing rules for mutable vectors
To modify a vector inside a function, we pass a mutable reference using&mut.Step 2: Identify correct function call syntax
Callingcounting_sort(&mut arr);passes a mutable reference correctly.Final Answer:
counting_sort(&mut arr); -> Option AQuick Check:
Mutable reference call = counting_sort(&mut arr); [OK]
- Passing vector without reference
- Passing immutable reference &arr
- Using invalid syntax like mut arr
let mut arr = vec![4, 2, 2, 8, 3, 3, 1];
counting_sort(&mut arr);
println!("{:?}", arr);Assuming
counting_sort correctly sorts the vector in ascending order.Solution
Step 1: Understand counting sort behavior
Counting sort sorts elements in ascending order, preserving duplicates.Step 2: Sort the given vector
Sorted vector is [1, 2, 2, 3, 3, 4, 8].Final Answer:
[1, 2, 2, 3, 3, 4, 8] -> Option BQuick Check:
Sorted ascending = [1, 2, 2, 3, 3, 4, 8] [OK]
- Reversing order to descending
- Removing duplicates incorrectly
- Printing original unsorted array
fn counting_sort(arr: Vec<i32>) {
// sorting logic
}
fn main() {
let mut data = vec![3, 1, 4];
counting_sort(data);
println!("{:?}", data);
}Solution
Step 1: Identify ownership move in function call
Passingdataby value moves ownership tocounting_sort.Step 2: Understand why println! causes error
After move,datais invalid inmain, so printing causes compilation error.Final Answer:
Cannot print data after move error -> Option CQuick Check:
Ownership moved, data invalid after call [OK]
- Assuming mutable keyword fixes ownership move
- Thinking missing return type causes error
- Blaming println! syntax
Solution
Step 1: Analyze dataset characteristics
Dataset has integers in range 0 to 1000, so k=1001 is small compared to n.Step 2: Compare sorting algorithm complexities
Counting Sort runs in O(n + k), which is efficient for small k. Quick, Merge are O(n log n). Bubble Sort is slow O(n²).Step 3: Choose best approach
Counting Sort is more efficient here due to linear time with small range.Final Answer:
Counting Sort is best due to O(n + k) time with small range -> Option AQuick Check:
Small range + large data = Counting Sort best [OK]
- Choosing Quick Sort ignoring range advantage
- Picking Bubble Sort for large data
- Ignoring Counting Sort's linear time benefit
Ready to prove it?
Pass the test and this pattern moves unknown
