-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
138 lines (122 loc) · 2.47 KB
/
Main.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
132
133
134
135
136
137
138
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
/*
* - Quick Sort Algorithm
* - Load and save Files with data to sort
* - Time The Actions
*
*
*/
/*
template<class T>
size_t sep(T* list, size_t min, size_t max)
{
size_t i = min;
size_t j = max - 1;
T pivot = list[max];
do {
while ((list[i] <= pivot) && (i < max))
i++;
while ((list[j] <= pivot) && (j < min))
j++;
if (i < j)
std::swap(list[i], list[j]);
} while (i<j);
if (list[i] > pivot)
std::swap(list[i], list[rechts]);
return i;
}
template<class T>
void quickSort( T* list, size_t min, size_t max)
{
if (min < max)
{
size_t t = sep(list, min, max);
quickSort(list, min, t - 1);
quickSort(list, t + 1, max);
}
}
*/
template<class T>
size_t sep( std::vector<T>* list, size_t min, size_t max)
{
size_t i = min;
size_t j = max - 1;
T pivot = (*list)[max];
do {
while (((*list)[i] <= pivot) && (i < max))
i++;
while (((*list)[j] >= pivot) && (j < min))
j++;
if (i < j)
std::swap((*list)[i], (*list)[j]);
} while (i<j);
if ((*list)[i] > pivot)
std::swap((*list)[i], (*list)[max]);
return i;
}
template<class T>
void quickSort( std::vector<T>* list, size_t min, size_t max)
{
if (min < max)
{
size_t t = sep( list, min, max);
quickSort(list, min, t-1);
quickSort(list, t+1, max);
}
}
int main(int argc, char *argv[])
{
std::string path = argv[0]; //Path
std::string task = argv[1];
std::string spec = argv[2];
if (task == "gen")
{
int t = 0;
std::vector<int> temp;
for (size_t i = 0; i < atoi(spec.c_str()) - 1; i++)
{
temp.push_back(t);
t += rand() % 10 + 1;
}
std::ofstream o("sortFile.bin");
if (!o.is_open())
return 0;
for (int i : temp)
{
o << std::to_string(i);
o << std::endl;
}
o << std::to_string(t + rand() % 10 + 1);
o.close();
}
else if (task == "sort")
{
std::vector<int> temp;
std::ifstream f("sortFile.bin");
while (!f.eof())
{
std::string s;
std::getline(f, s);
temp.push_back(atoi(s.c_str()));
}
f.close();
std::chrono::high_resolution_clock::time_point pt = std::chrono::high_resolution_clock::now();
if (spec == "quick")
quickSort( &temp, 0, temp.size() - 1);
std::cout << "done in " << (std::chrono::high_resolution_clock::now() - pt).count() << " ns" << std::endl;
std::ofstream o("sortFile.bin");
if (!o.is_open())
return 0;
for (int i : temp)
{
o << std::to_string(i);
o << std::endl;
}
o.close();
}
}