Skip to content

Commit

Permalink
Merge pull request #18 from VadimYarovoy/feat/sortings_tasks
Browse files Browse the repository at this point in the history
merge: Add missed sorting task
  • Loading branch information
VadimYarovoy authored Aug 20, 2024
2 parents 2fa912a + e1c0d1e commit 2b59638
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/sortings/leetcode_75.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[allow(dead_code)]
pub fn sort_colors(nums: &mut Vec<i32>) {
let mut counter = [0; 3];
for &n in nums.iter() {
counter[n as usize] += 1;
}

let mut curr_idx = 0;
for i in 0..3 {
for _ in 0..counter[i] {
nums[curr_idx] = i as i32;
curr_idx += 1;
}
}
}

#[cfg(test)]
mod sort_colors_tests {
use super::sort_colors;

#[test]
fn sort_some_colors() {
let mut colors = vec![2, 0, 2, 1, 1, 0];
sort_colors(&mut colors);
assert_eq!(colors, vec![0, 0, 1, 1, 2, 2]);
}
}
1 change: 1 addition & 0 deletions src/sortings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod insertion_sort;
mod leetcode_75;
mod merge_sort;
mod quick_sort;

0 comments on commit 2b59638

Please sign in to comment.