-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathheapsort.cpp
83 lines (72 loc) · 2 KB
/
heapsort.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* To compile:
g++ heapsort.cpp timer.cpp -o heapsort
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "timer.h"
void maxHeapify(int heap[],int n);
void buildMaxHeap(int heap[],int n);
void heapSort(int heap[],int n);
//In C/C++, any statement that begins with # is a pre-processor statement
//pre-processor statements are handled before compilation by the preprocessor
//it essentially generates more source code depending on the statement and it is
//this altered source that gets compiled.
//#define allows us to create labels and macros.
//we define the symbol (LEFT(idx)) and what it should be replaced with ((idx) * 2 + 1)
//the preprocessor will go through the source code and replace every LEFT(idx) with (idx) *2+1
//This is good for simple expressions and will speed up the program
//as they look like a function call within the program but
//does not have the expense of the function call
#define LEFT(idx) (idx) * 2 + 1
#define RIGHT(idx) (idx) * 2 + 2
int leftIdx(int idx);
int rightIdx(int idx);
int main(int argc, char* argv[]){
int size=atoi(argv[1]);
int* myarr=new int[size];
std::ofstream log("heap.log");
Timer stopwatch;
for(int i=0;i<size;i++){
myarr[i]=rand();
}
stopwatch.start();
heapSort(myarr,size);
stopwatch.stop();
std::cout << stopwatch.currtime() << std::endl;
for(int i=0;i<size;i++){
log <<myarr[i]<< std::endl;
}
return 0;
}
void maxHeapify(int heap[],int idx,int n){
int left=LEFT(idx);
int right=RIGHT(idx);
int largest=idx;
if(left < n && heap[left] > heap[largest]){
largest=left;
}
if(right < n && heap[right] > heap[largest]){
largest=right;
}
if(largest!=idx){
int tmp=heap[idx];
heap[idx]=heap[largest];
heap[largest]=tmp;
maxHeapify(heap,largest,n);
}
}
void buildMaxHeap(int heap[],int n){
for(int i=(n-1)/2;i>=0;i--){
maxHeapify(heap,i,n);
}
}
void heapSort(int heap[],int n){
buildMaxHeap(heap,n);
for(int i=n-1;i>0;i--){
int tmp=heap[0];
heap[0]=heap[i];
heap[i]=tmp;
maxHeapify(heap,0,i);
}
}