forked from alexfertel/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitonic_sort.rs
85 lines (71 loc) · 1.95 KB
/
bitonic_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
pub fn bitonic_sort(up: bool, x: &mut [i32]) {
if x.len() <= 1 {
return;
}
let mid = x.len() / 2;
let (first, second) = x.split_at_mut(mid);
bitonic_sort(true, first);
bitonic_sort(false, second);
bitonic_merge(up, x);
}
pub struct BitonicSort;
fn bitonic_merge(up: bool, x: &mut [i32]) {
if x.len() <= 1 {
return;
}
let mid = x.len() / 2;
for i in 0..mid {
if up == (x[i] > x[mid + i]) {
x.swap(i, mid + i);
}
}
let (first, second) = x.split_at_mut(mid);
bitonic_merge(up, first);
bitonic_merge(up, second);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bitonic_sort() {
let mut numbers = vec![10, 30, 11, 20, 4, 330, 21, 110];
bitonic_sort(true, &mut numbers);
assert_eq!(numbers, vec![4, 10, 11, 20, 21, 30, 110, 330]);
}
#[test]
fn test_bitonic_sort_empty() {
let mut numbers = vec![];
bitonic_sort(true, &mut numbers);
assert_eq!(numbers, vec![]);
}
#[test]
fn test_bitonic_sort_one_element() {
let mut numbers = vec![10];
bitonic_sort(true, &mut numbers);
assert_eq!(numbers, vec![10]);
}
#[test]
fn test_bitonic_sort_two_elements() {
let mut numbers = vec![10, 30];
bitonic_sort(true, &mut numbers);
assert_eq!(numbers, vec![10, 30]);
}
#[test]
fn test_error_bitonic_sort() {
let mut numbers = vec![10, 30, 11, 20, 4, 330, 21, 110];
bitonic_sort(true, &mut numbers);
assert_ne!(numbers, vec![10, 4, 11, 20, 21, 30, 110, 330]);
}
#[test]
fn test_bitonic_merge_empty() {
let mut numbers = vec![];
bitonic_merge(true, &mut numbers);
assert_eq!(numbers, vec![]);
}
#[test]
fn test_bitonic_merge_one_element() {
let mut numbers = vec![10];
bitonic_merge(true, &mut numbers);
assert_eq!(numbers, vec![10]);
}
}