-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
91 lines (74 loc) · 1.6 KB
/
main.cpp
File metadata and controls
91 lines (74 loc) · 1.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <fstream>
#include <cmath>
#include <chrono>
#include <cstring>
#include <iomanip>
using namespace std;
void sink(int *arr, int ind, int m)
{
int big, l, r;
while (ind < m)
{
big = ind;
l = 2 * ind + 1;
r = l + 1;
if (l < m && arr[l] > arr[big])
big = l;
if (r < m && arr[r] > arr[big])
big = r;
if (big == ind)
return;
swap(arr[ind], arr[big]);
ind = big;
}
}
void build_heap(int *arr, int n)
{
int ind = n / 2 - 1;
while (ind >= 0)
{
sink(arr, ind, n);
ind--;
}
}
void heapSort(int *arr, int n)
{
build_heap(arr, n);
int end = n - 1;
while (end >= 0)
{
swap(arr[0], arr[end]);
sink(arr, 0, end);
end--;
}
}
int main(int argc, char **argv)
{
if (argc < 3)
return -1;
ifstream inp(argv[1]);
int current_number = 0, n, i = 0;
inp >> n;
int *arr = (int *)malloc(n * sizeof(int));
while (inp >> current_number)
{
arr[i] = current_number;
i++;
}
inp.close();
auto start = chrono::steady_clock::now();
heapSort(arr, n);
auto end = chrono::steady_clock::now();
auto diff = end - start;
ofstream out(argv[2], std::ios::out | std::ios::trunc);
cout << "[DEBUG] Error: " << strerror(errno) << endl;
cout << "[DEBUG] File path: " << argv[2] << endl;
out << chrono::duration<double, milli>(diff).count() << "\n";
for (int i = 0; i < n; i++)
{
out << arr[i] << " ";
}
out.close();
return 0;
}