-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathbead_sort.rs
59 lines (52 loc) · 1.35 KB
/
bead_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
57
58
59
//Bead sort only works for sequences of non-negative integers.
//https://en.wikipedia.org/wiki/Bead_sort
pub fn bead_sort(a: &mut [usize]) {
// Find the maximum element
let mut max = a[0];
(1..a.len()).for_each(|i| {
if a[i] > max {
max = a[i];
}
});
// allocating memory
let mut beads = vec![vec![0; max]; a.len()];
// mark the beads
for i in 0..a.len() {
for j in (0..a[i]).rev() {
beads[i][j] = 1;
}
}
// move down the beads
for j in 0..max {
let mut sum = 0;
(0..a.len()).for_each(|i| {
sum += beads[i][j];
beads[i][j] = 0;
});
for k in ((a.len() - sum)..a.len()).rev() {
a[k] = j + 1;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sorting::have_same_elements;
use crate::sorting::is_sorted;
#[test]
fn descending() {
//descending
let mut ve1: [usize; 5] = [5, 4, 3, 2, 1];
let cloned = ve1;
bead_sort(&mut ve1);
assert!(is_sorted(&ve1) && have_same_elements(&ve1, &cloned));
}
#[test]
fn mix_values() {
//pre-sorted
let mut ve2: [usize; 5] = [7, 9, 6, 2, 3];
let cloned = ve2;
bead_sort(&mut ve2);
assert!(is_sorted(&ve2) && have_same_elements(&ve2, &cloned));
}
}