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
use super::{Algorithm, Array};
pub struct HeapSort;
impl Algorithm for HeapSort {
fn sort(&self, array: Array) {
let first = 0;
let n = array.len();
for i in (first..n / 2 - 1).rev() {
heapify(&array, n, i);
}
for i in (first..n).rev() {
array.swap(first, i);
heapify(&array, i, first);
}
fn heapify(array: &Array, n: usize, i: usize) {
let mut largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if left < n && array.get(left) > array.get(largest) {
largest = left;
}
if right < n && array.get(right) > array.get(largest) {
largest = right;
}
if largest != i {
array.set_color(i, [0.0, 1.0, 0.0, 0.8]);
array.swap(i, largest);
heapify(array, n, largest);
array.wait(5);
array.reset_color(i);
}
}
}
fn name(&self) -> String {
"HeapSort sort".to_string()
}
}