-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path6-heap-sort.rs
51 lines (39 loc) · 1.19 KB
/
6-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
// Heap sort
mod sort {
/// Sort the given array via heap sort
/// Complexity: O(array.len()*log2(array.len()))
pub fn heap_sort(array: &mut Vec<i32>) {
for i in (0..array.len() / 2).rev() {
hepify(array, array.len(), i);
}
for i in (0..array.len()).rev() {
array.swap(i, 0);
hepify(array, i, 0);
}
}
fn hepify(array: &mut Vec<i32>, n: usize, node: usize) {
let left = 2 * node + 1;
let right = 2 * node + 2;
let mut new_node = node;
if left < n && array[left] > array[new_node] {
new_node = left;
}
if right < n && array[right] > array[new_node] {
new_node = right;
}
if new_node != node {
array.swap(node, new_node);
hepify(array, n, new_node);
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_heap_sort() {
let mut test_array: Vec<i32> = vec![5, 1, 4, 7, 13, 2];
let expected_result: Vec<i32> = vec![1, 2, 4, 5, 7, 13];
crate::sort::heap_sort(&mut test_array);
assert_eq!(expected_result, test_array);
}
}