-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (57 loc) · 1.46 KB
/
main.cpp
File metadata and controls
61 lines (57 loc) · 1.46 KB
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
#include <iostream>
#include <cstdlib>
#include <chrono>
#include "heap.h"
#include <assert.h>
using namespace std;
using namespace std::chrono;
bool isHeap(int arr[], int parent)
{
if (parent != 1 && arr[parent] == 0)
return true;
return arr[parent] <= arr[2 * parent] && arr[parent] <= arr[2 * parent + 1] &&
isHeap(arr, 2 * parent) && isHeap(arr, 2 * parent + 1);
}
void heapSort(int arr[], int n)
{
Heap send(n);
for (int i = 0; i < n; i++)
send.insert(arr[n]);
assert(isHeap(send.harry, 1));
for (int j = 0; j < n; j++) {
arr[j] = send.min();
send.remove_min();
}
}
int main()
{
Heap h(900);
srand(time(0));
int v;
int n;
high_resolution_clock::time_point t1;
high_resolution_clock::time_point t2;
t1 = high_resolution_clock::now();
t2 = high_resolution_clock::now();
double total = 0;
duration<double> time_span = duration_cast<duration<double> >(t2-t1);
for (int j = 0; j < 10; j++) // number of trials
{
n = rand() % 30000; // number of elements
// n = 900;
int *arr = new int[n];
for (int i = 0; i < n; i++)
{
v = rand() % 1000; // number to be inserted
arr[i] = v;
}
t1 = high_resolution_clock::now();
heapSort(arr, n);
t2 = high_resolution_clock::now();
time_span = duration_cast<duration<double> >(t2 - t1);
total = time_span.count();
// cout << n << ", " << total << endl;
delete[] arr;
h.reset();
}
}