-
Notifications
You must be signed in to change notification settings - Fork 21
/
heap.cpp
57 lines (46 loc) · 1.27 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// Created by light on 19-10-29.
//
#include <cstdlib>
#include <climits>
#include <iostream>
#include "heap.h"
#include "PriorityQueue.h"
double testHeap(const vector<int> &testData, bool isHeapify) {
time_t startTime = clock();
MaxHeap<int> *maxHeap;
if (isHeapify)
maxHeap = new MaxHeap<int>(testData);
else {
maxHeap = new MaxHeap<int>();
for (auto num:testData)
maxHeap->add(num);
}
vector<int> res(testData.size());
for (int i = 0; i < testData.size(); i++) {
res[i] = maxHeap->extractMax();
}
for (int i = 1; i < testData.size(); i++) {
if (res[i - 1] < res[i]) throw "Error";
}
cout << "Test MaxHeap completed." << endl;
delete maxHeap;
time_t endTime = clock();
return double(endTime - startTime) / CLOCKS_PER_SEC;
}
int main() {
int n = 10000;
vector<int> testData(n);
for (int i = 0; i < n; i++) {
int r = rand() % (INT_MAX + 1);
testData.push_back(r);
}
// O(nlog(n))
double time1 = testHeap(testData, false);
cout << "without heapify " << time1 << " s" << endl;
// O(n)
double time2 = testHeap(testData, true);
cout << "with heapify " << time2 << " s" << endl;
PriorityQueue<int> priorityQueue;
return 0;
}