From e9201ef326132612b5ed998f3193a024ea4aaacc Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Mon, 20 May 2024 19:11:57 +0800 Subject: [PATCH] sorts: Add unittests for bucket sort --- sort/src/bucket_sort.rs | 45 +++++++++++++++++++++++++++++++++------ sort/src/counting_sort.rs | 29 +++++++++++++++++++++++++ sort/src/lib.rs | 3 ++- sort/src/merge_sort.rs | 2 -- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/sort/src/bucket_sort.rs b/sort/src/bucket_sort.rs index 1a1151491..8d21aeba3 100644 --- a/sort/src/bucket_sort.rs +++ b/sort/src/bucket_sort.rs @@ -3,8 +3,10 @@ // in the LICENSE file. pub fn bucket_sort(nums: &mut [i32]) { - let bucket_size = nums.len().ilog2() as usize; - bucket_sort_with_bucket(nums, bucket_size); + if !nums.is_empty() { + let bucket_size = nums.len().ilog2() as usize; + bucket_sort_with_bucket(nums, bucket_size); + } } fn insertion_sort(nums: &mut [i32]) { @@ -21,14 +23,16 @@ fn insertion_sort(nums: &mut [i32]) { #[allow(clippy::cast_sign_loss)] fn bucket_sort_with_bucket(nums: &mut [i32], bucket_size: usize) { - let min_num: i32 = nums.iter().min().copied().unwrap_or_default(); - let max_num: i32 = nums.iter().max().copied().unwrap_or_default(); - let bucket_count: usize = ((max_num - min_num) as usize) / bucket_size + 1; + let min_num: i32 = nums.iter().min().copied().unwrap(); + let max_num: i32 = nums.iter().max().copied().unwrap(); + let range: i32 = max_num - min_num; + let bucket_count: usize = range as usize / bucket_size + 1; let mut buckets: Vec> = vec![vec![]; bucket_count]; // 遍历数组, 将元素分配到每个桶中 for &num in nums.iter() { - let bucket_index = ((num - min_num) as usize) / bucket_size; + let range: i32 = num - min_num; + let bucket_index: usize = range as usize / bucket_size; buckets[bucket_index].push(num); } @@ -40,3 +44,32 @@ fn bucket_sort_with_bucket(nums: &mut [i32], bucket_size: usize) { index += bucket.len(); } } + +#[cfg(test)] +mod tests { + use super::bucket_sort; + + #[test] + fn test_bucket_sort() { + let mut list = [0, 5, 3, 2, 2]; + bucket_sort(&mut list); + assert_eq!(list, [0, 2, 2, 3, 5]); + + let mut list = [-2, -5, -45]; + bucket_sort(&mut list); + assert_eq!(list, [-45, -5, -2]); + + let mut list = [ + -998_166, -996_360, -995_703, -995_238, -995_066, -994_740, -992_987, -983_833, + -987_905, -980_069, -977_640, + ]; + bucket_sort(&mut list); + assert_eq!( + list, + [ + -998_166, -996_360, -995_703, -995_238, -995_066, -994_740, -992_987, -987_905, + -983_833, -980_069, -977_640, + ] + ); + } +} diff --git a/sort/src/counting_sort.rs b/sort/src/counting_sort.rs index d8b685196..b91a511fd 100644 --- a/sort/src/counting_sort.rs +++ b/sort/src/counting_sort.rs @@ -88,3 +88,32 @@ pub fn counting_sort(nums: &mut [i32]) { //counts[index] -= 1; } } + +#[cfg(test)] +mod tests { + use super::counting_sort; + + #[test] + fn test_counting_sort() { + let mut list = [0, 5, 3, 2, 2]; + counting_sort(&mut list); + assert_eq!(list, [0, 2, 2, 3, 5]); + + let mut list = [-2, -5, -45]; + counting_sort(&mut list); + assert_eq!(list, [-45, -5, -2]); + + let mut list = [ + -998_166, -996_360, -995_703, -995_238, -995_066, -994_740, -992_987, -983_833, + -987_905, -980_069, -977_640, + ]; + counting_sort(&mut list); + assert_eq!( + list, + [ + -998_166, -996_360, -995_703, -995_238, -995_066, -994_740, -992_987, -987_905, + -983_833, -980_069, -977_640, + ] + ); + } +} diff --git a/sort/src/lib.rs b/sort/src/lib.rs index 6966b380c..bc3d7ece0 100644 --- a/sort/src/lib.rs +++ b/sort/src/lib.rs @@ -23,7 +23,6 @@ mod quick_sort; mod selection_sort; mod shaker_sort; mod shell_sort; -pub mod util; pub use bubble_sort::bubble_sort; pub use bucket_sort::bucket_sort; @@ -37,3 +36,5 @@ pub use quick_sort::quick_sort; pub use selection_sort::selection_sort; pub use shaker_sort::shaker_sort; pub use shell_sort::shell_sort; + +pub mod util; diff --git a/sort/src/merge_sort.rs b/sort/src/merge_sort.rs index cbc14d39b..075b24ba8 100644 --- a/sort/src/merge_sort.rs +++ b/sort/src/merge_sort.rs @@ -30,7 +30,6 @@ fn sort(arr: &mut [T], low: usize, high: usize) where T: PartialOrd + Copy, { - println!("low: {low}, high: {high}"); if low >= high { return; } @@ -62,7 +61,6 @@ fn merge(arr: &mut [T], low: usize, middle: usize, high: usize) where T: PartialOrd + Copy, { - println!("merge: low: {low}, middle: {middle}, high: {high}"); let aux = arr[0..=high].to_vec(); // Merge back to arr.