Skip to content

Commit

Permalink
sorts: Add unittests for bucket sort
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 20, 2024
1 parent 0a041a1 commit e9201ef
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
45 changes: 39 additions & 6 deletions sort/src/bucket_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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<i32>> = 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);
}

Expand All @@ -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,
]
);
}
}
29 changes: 29 additions & 0 deletions sort/src/counting_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
}
3 changes: 2 additions & 1 deletion sort/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
2 changes: 0 additions & 2 deletions sort/src/merge_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn sort<T>(arr: &mut [T], low: usize, high: usize)
where
T: PartialOrd + Copy,
{
println!("low: {low}, high: {high}");
if low >= high {
return;
}
Expand Down Expand Up @@ -62,7 +61,6 @@ fn merge<T>(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.
Expand Down

0 comments on commit e9201ef

Please sign in to comment.