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
use super::{Algorithm, Array};
pub struct CocktailSort;
impl Algorithm for CocktailSort {
fn sort(&self, array: Array) {
let len = array.len();
let mut first = 0;
let mut last = len - 1;
let mut swapped: bool = true;
while swapped {
swapped = false;
array.set_color(last, [0.0, 1.0, 0.0, 0.8]);
for i in first..last {
array.set_color(i, [0.0, 1.0, 0.0, 0.8]);
if array.get(i) > array.get(i + 1) {
array.swap(i, i + 1);
swapped = true;
}
array.wait(5);
array.reset_color(i);
}
array.reset_color(last);
if !swapped {
break;
}
swapped = false;
last -= 1;
array.set_color(first, [0.0, 1.0, 0.0, 0.8]);
for j in (first..last).rev() {
array.set_color(j, [0.0, 1.0, 0.0, 0.8]);
if array.get(j) > array.get(j + 1) {
array.swap(j, j + 1);
swapped = true;
}
array.wait(5);
array.reset_color(j);
}
array.reset_color(first);
first += 1;
}
}
fn name(&self) -> String {
"Cocktail sort".to_string()
}
}