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
pub fn comb_sort<T: PartialOrd>(input: &mut [T]) {
if input.len() < 2 {return;}
let len = input.len();
let inv_shrink: f32 = 1.0 / 1.3;
let mut gap = len;
let mut sorted = len < 2;
while !sorted {
gap = (gap as f32 * inv_shrink).floor() as usize;
if gap <= 1 {
gap = 1;
sorted = true;
}
for i in 0..len - gap {
if input[i] > input[i + gap] {
input.swap(i, i + gap);
sorted = false;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_comb() {
let mut vector_in = vec![30, 10, 20, 11, 24, 44, 12, 11];
comb_sort(&mut vector_in);
debug_assert_eq!(vector_in, vec![10, 11, 11, 12, 20, 24, 30, 44]);
}
#[test]
fn test_comb_empty() {
let mut vector_in:Vec<i32> = vec![];
comb_sort(&mut vector_in);
debug_assert_eq!(vector_in, &[]);
}
#[test]
fn test_comb_len1() {
let mut vector_in = vec![1];
comb_sort(&mut vector_in);
debug_assert_eq!(vector_in, vec![1]);
}
}