š Scenario: You work in a warehouse where you need to organize boxes by their weight. Some boxes have weights that can be compared directly, while others have weights represented as categories. You want to learn two ways to sort these boxes: by comparing weights directly and by using a counting method for categories.
šÆ Goal: Build two sorting methods in Rust: one that sorts a list of numbers by comparing them, and another that sorts a list of categories using counting. Then print the sorted results.
š What You'll Learn
Create a vector called weights with the numbers 40, 10, 50, 20, 30
Create a vector called categories with the strings "small", "large", "medium", "small", "large"
Sort the weights vector using comparison-based sorting
Sort the categories vector using counting sort logic
Print both sorted vectors
š” Why This Matters
š Real World
Sorting is used everywhere, like organizing boxes by weight or grouping items by category in warehouses or stores.
š¼ Career
Understanding different sorting methods helps in choosing the best approach for data processing tasks in software development and data analysis.
Progress0 / 4 steps
1
Create the initial data vectors
Create a vector called weights with the numbers 40, 10, 50, 20, 30 and a vector called categories with the strings "small", "large", "medium", "small", "large".
DSA Rust
Hint
Use vec![...] to create vectors with exact values.
2
Prepare a count map for categories
Create a mutable HashMap called count_map to count how many times each category appears in categories. Use std::collections::HashMap.
DSA Rust
Hint
Import HashMap and create it with HashMap::new().
3
Sort weights and count categories
Sort the weights vector using weights.sort(). Then use a for loop with variable category to iterate over categories.iter() and update count_map by increasing the count for each category.
DSA Rust
Hint
Use weights.sort() to sort numbers. Use for category in categories.iter() to loop. Use count_map.entry(category).or_insert(0) to count.
4
Print the sorted weights and sorted categories
Print the sorted weights vector using println!("Sorted weights: {:?}", weights). Then create a new vector sorted_categories by repeating each category from ["small", "medium", "large"] according to its count in count_map. Finally, print sorted_categories using println!("Sorted categories: {:?}", sorted_categories).
DSA Rust
Hint
Print the sorted weights vector. Then build sorted_categories by repeating each category by its count. Print sorted_categories.
Practice
(1/5)
1. Which of the following sorting algorithms is non-comparison based?
easy
A. Heap Sort
B. Merge Sort
C. Quick Sort
D. Counting Sort
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 D
Quick Check:
Non-comparison based = Counting Sort [OK]
Hint: Non-comparison sorts use element values, not comparisons [OK]
Common Mistakes:
Confusing Merge Sort as non-comparison
Thinking Quick Sort is non-comparison
Assuming Heap Sort is non-comparison
2. Which of the following is the correct Rust syntax to call a non-comparison based sorting function counting_sort on a mutable vector arr?
easy
A. counting_sort(&mut arr);
B. counting_sort(arr);
C. counting_sort(&arr);
D. counting_sort(mut 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
Calling counting_sort(&mut arr); passes a mutable reference correctly.