implemented heap sort
This commit is contained in:
parent
5b33b2d651
commit
43b45b31fd
2 changed files with 23 additions and 2 deletions
|
|
@ -1,3 +1,14 @@
|
|||
pub fn heap_sort(){
|
||||
|
||||
use std::collections::BinaryHeap;
|
||||
|
||||
pub fn heap_sort(arr: &mut Vec<u64>) {
|
||||
let mut heap = BinaryHeap::new();
|
||||
for i in arr.iter_mut() {
|
||||
heap.push(*i);
|
||||
}
|
||||
for i in 0..arr.len() {
|
||||
if let Some(val) = heap.pop() {
|
||||
arr[i] = val;
|
||||
}
|
||||
}
|
||||
arr.reverse()
|
||||
}
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -79,6 +79,15 @@ fn radix_sort_demo(){
|
|||
println!("Array before radix sort: {:?}",arr);
|
||||
println!("Array after radix sort: {:?}",radix_sort::radix_sort(arr));
|
||||
}
|
||||
fn heap_sort_demo(){
|
||||
println!("-------------------");
|
||||
println!("HEAP SORT DEMO:");
|
||||
println!("-------------------");
|
||||
let mut arr:Vec<u64> = vec![9, 3, 7, 4, 69, 420, 42];
|
||||
println!("Array before heap sort: {:?}",arr);
|
||||
heap_sort::heap_sort(&mut arr);
|
||||
println!("Array after heap sort: {:?}",arr);
|
||||
}
|
||||
fn main() {
|
||||
println!("");
|
||||
println!("|~~~~~~~~~~~~~~~|");
|
||||
|
|
@ -96,4 +105,5 @@ fn main() {
|
|||
bubble_sort_demo();
|
||||
merge_sort_demo();
|
||||
radix_sort_demo();
|
||||
heap_sort_demo();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue