rust_dsa

Function heapsort

Source
pub fn heapsort<T>(slice: &mut [T])
where T: Ord,
Expand description

An implementation of the heapsort algorithm.

ยงExample

use rust_dsa::heapsort;

let mut ints = [42, 14, 2, 18, 33, 19, 21, 38];
heapsort(&mut ints);
assert_eq!(&ints, &[2, 14, 18, 19, 21, 33, 38, 42]);

let mut food = ["banana", "eggplant", "dragonfruit", "apple", "carrot"];
heapsort(&mut food);
assert_eq!(&food, &["apple", "banana", "carrot", "dragonfruit", "eggplant"]);

let mut random: Vec<i64> = (0..100_000).map(|_| rand::random()).collect();
heapsort(&mut random);
for i in 1..random.len() {
    assert!(random[i - 1] <= random[i]);
}