-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort-vector.cpp
131 lines (114 loc) · 2.51 KB
/
Quicksort-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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include<vector>
#include<iomanip>
#include <fstream>
#include <string>
#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 QuickSort(vector<double>&, int, int);
int Partition(vector<double>&, int, int);
int main()
{
for (int i = 8; i <= 8; 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();
QuickSort(b, 0, b.size() - 1);
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;
}
int Xuat(vector<double> a)
{
for (size_t i = 0; i < a.size(); i++)
cout << setw(7) << setprecision(2) << fixed << a[i];
return 1;
}
int Partition(vector<double>& values, int left, int right)
{
int pivotIndex = left + (right - left) / 2;
int pivotValue = values[pivotIndex];
int i = left, j = right;
while (i <= j) {
while (values[i] < pivotValue) {
i++;
}
while (values[j] > pivotValue) {
j--;
}
if (i <= j) {
swap(values[i], values[j]);
i++;
j--;
}
}
return i;
}
void QuickSort(vector<double>& a, int Left, int Right)
{
while (Left < Right)
{
int iPivot = Partition(a, Left, Right);
if (iPivot - Left <= Right - (iPivot + 1))
{
QuickSort(a, Left, iPivot - 1);
Left = iPivot + 1;
}
else
{
QuickSort(a, iPivot, Right);
Right = iPivot - 1;
}
}
}