-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path7-counting-sort.rs
56 lines (44 loc) · 1.4 KB
/
7-counting-sort.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Counting sort
mod sort {
use std::cmp;
/// Sort the given array via counting sort
///
/// Complexity: If the numbers in the array are in the range [a, b]
/// then the complexity is O(b - a).
pub fn counting_sort(array: &mut Vec<i32>) {
let mut offset = i32::MAX;
let mut max_element = i32::MIN;
for num in array.iter() {
max_element = cmp::max(max_element, *num);
offset = cmp::min(max_element, *num);
}
if offset < 0 {
offset = -offset;
} else {
offset = 0;
}
let mut count: Vec<i32> = vec![0; (max_element + offset + 1) as usize];
for num in array.iter() {
let index = (*num + offset) as usize;
count[index] = count[index] + 1;
}
let mut index: usize = 0;
for num in 0..count.len() {
while count[num] > 0 {
array[index] = (num as i32) - offset;
count[num] = count[num] - 1;
index = index + 1;
}
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_counting_sort() {
let mut test_array: Vec<i32> = vec![5, 1, 4, 7, 13, 2];
let expected_result: Vec<i32> = vec![1, 2, 4, 5, 7, 13];
crate::sort::counting_sort(&mut test_array);
assert_eq!(expected_result, test_array);
}
}