-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap_sort_vector.cpp
113 lines (100 loc) · 2.19 KB
/
Heap_sort_vector.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include<chrono>
using namespace std;
using namespace std::chrono;
int Nhap(vector<double>&, int&, string);
int Xuat(vector<double>);
int Xuat(vector<double>, string);
void BuildHeap(vector<double>, int);
void Heapify(vector<double>, int, int);
void HeapSort(vector<double>, int);
int main()
{
for (int i = 2; i <= 2; i++)
{
vector<double> b;
int k;
string filename = "data";
if (i < 10)
filename += '0';
filename += to_string(i);
string filenameinp = filename;
filenameinp += ".txt";
if (Nhap(b, k, filenameinp) == 1)
{
auto start = high_resolution_clock::now();
HeapSort(b, b.size());
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " millisecond" << endl;
string filenameout = filename;
filenameout += ".out";
cout << "Xuat ra file: ";
Xuat(b, filenameout);
cout << filenameout << " Done ";
cout << endl;
}
else
cout << "\n Khong mo duoc file " << filename << "\n";
}
cout << "\n End Game ";
cout << "\n\n\n";
return 1;
}
int Nhap(vector<double>& a, int& n, string filename)
{
ifstream fi(filename);
if (fi.fail() == true)
return 0;
fi >> n;
for (int i = 0; i < n; i++)
{
double temp;
fi >> temp;
a.push_back(temp);
}
return 1;
}
int Xuat(vector<double> a, string filename)
{
ofstream fo(filename);
if (fo.fail() == true)
return 0;
fo << a.size() << endl;
for (size_t i = 0; i < a.size(); i++)
fo << setw(5) << a[i];
return 1;
}
void BuildHeap(vector<double> a, int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
Heapify(a, n, i);
}
void Heapify(vector<double> a, int n, int vt)
{
while (vt <= n / 2 - 1)
{
int lc = 2 * vt + 1;
if (lc + 1 < n && a[lc] < a[lc + 1])
lc++;
if (a[vt] < a[lc])
swap(a[vt], a[lc]);
vt = lc;
}
}
void HeapSort(vector<double> a, int n)
{
BuildHeap(a, n);
int length = n;
while (length > 1)
{
swap(a[0], a[length - 1]);
length--;
Heapify(a, length, 0);
}
}