September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,39 @@
|
|||
fn main() {
|
||||
let mut v = [4,6,8,1,0,3,2,2,9,5];
|
||||
heap_sort(&mut v, |x,y| x < y);
|
||||
println!("{:?}", v);
|
||||
}
|
||||
|
||||
fn heap_sort<T,F>(array: &mut [T], order: F)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
{
|
||||
let len = array.len();
|
||||
// Create heap
|
||||
for start in (0..len/2).rev() {
|
||||
sift_down(array,&order,start,len-1)
|
||||
}
|
||||
|
||||
for end in (1..len).rev() {
|
||||
array.swap(0,end);
|
||||
sift_down(array,&order,0,end-1)
|
||||
}
|
||||
}
|
||||
|
||||
fn sift_down<T,F>(array: &mut [T], order: &F, start: usize, end: usize)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
{
|
||||
let mut root = start;
|
||||
loop {
|
||||
let mut child = root * 2 + 1;
|
||||
if child > end { break; }
|
||||
if child + 1 <= end && order(&array[child], &array[child + 1]) {
|
||||
child += 1;
|
||||
}
|
||||
if order(&array[root], &array[child]) {
|
||||
array.swap(root,child);
|
||||
root = child
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
use std::collections::BinaryHeap;
|
||||
|
||||
fn main() {
|
||||
let src = vec![6,2,3,6,1,2,7,8,3,2];
|
||||
let sorted= BinaryHeap::from(src).into_sorted_vec();
|
||||
println!("{:?}", sorted);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue