-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
42 lines (39 loc) · 1.02 KB
/
heap.cpp
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
// Build a heap from an element and two proper heaps
#include "recorder.h"
// v2*top+1
// arr = [...,el,...,~,~, ...]
// ^top ^heads of proper heaps
void maxHeapify(Recorder rec, int top, int length){
while(top*2+1 < length){
int child = top*2+1;
if(rec.cmp(child,top)){
if(child+1 < length && rec.cmp(child+1,child)){
rec.swap(top,child+1);
top = child+1;
}else{
rec.swap(top,child);
top = child;
}
}else if(child+1 < length && rec.cmp(child+1,top)){
rec.swap(top,child+1);
top = child+1;
}else
break;
}
}
// Converts an array into a heap
void buildMaxHeap(Recorder rec, int length){
for(int i = (length+1)/2; i >= 0; i--){
maxHeapify(rec,i,length);
}
}
void heapSort(Recorder rec, int length){
buildMaxHeap(rec,length);
for(int i = length-1; i > 0; i--){
// Move the largest element to the end
rec.swap(0,i);
rec.finalize(i);
//Reestablish the heap property
maxHeapify(rec,0,i);
}
}