-
Notifications
You must be signed in to change notification settings - Fork 74
/
heap_sort.rs
64 lines (53 loc) · 1.27 KB
/
heap_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
use crate::sorting::traits::Sorter;
fn heap_sort<T: Ord>(array: &mut [T]) {
if array.len() < 2 {
return;
}
heapify(array);
let mut end = array.len() - 1;
while end > 0 {
array.swap(end, 0);
end -= 1;
siftdown(array, 0, end);
}
}
fn heapify<T: Ord>(array: &mut [T]) {
let start = (array.len() - 2) / 2;
for i in (0..start + 1).rev() {
siftdown(array, i, array.len() - 1);
}
}
fn siftdown<T: Ord>(array: &mut [T], mut root: usize, end: usize) {
while 2 * root < end {
let child = 2 * root + 1;
let mut swap = root;
if array[swap] < array[child] {
swap = child;
}
if child < end && array[swap] < array[child + 1] {
swap = child + 1;
}
if swap == root {
return;
} else {
array.swap(root, swap);
root = swap;
}
}
}
pub struct HeapSort;
impl<T> Sorter<T> for HeapSort
where
T: Ord + Copy,
{
fn sort_inplace(arr: &mut [T]) {
heap_sort(arr);
}
}
#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;
use crate::sorting::HeapSort;
sorting_tests!(HeapSort::sort, heap_sort);
sorting_tests!(HeapSort::sort_inplace, heap_sort_inplace, inplace);
}