-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (50 loc) · 1.66 KB
/
main.cpp
File metadata and controls
64 lines (50 loc) · 1.66 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
#include <vector>
#include <iostream>
#include <random>
#include <algorithm>
#include <chrono>
using namespace std;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> generateRandomVector(int length, int minVal, int maxVal) {
uniform_int_distribution<int> distribution(minVal, maxVal);
vector<int> result;
result.reserve(length);
for (int i = 0; i < length; ++i) {
result.push_back(distribution(gen));
}
return result;
}
void shakerSort(vector<int>& arr) {
bool swapped;
do {
swapped = false;
for (int i = 0; i < arr.size() - 1; ++i) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
swapped = true;
}
}
if (!swapped) break;
swapped = false;
for (int i = arr.size() - 2; i >= 0; --i) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
swapped = true;
}
}
} while (swapped);
}
int main() {
vector<int> randomVector = generateRandomVector(10000, 1, 10000);
auto start = chrono::high_resolution_clock::now();
shakerSort(randomVector);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "Time taken by Shaker sort: " << duration.count() << " seconds" << endl;
start = chrono::high_resolution_clock::now();
sort(randomVector.begin(), randomVector.end());
end = chrono::high_resolution_clock::now();
duration = end - start;
cout << "Time taken by std::sort: " << duration.count() << " seconds" << endl;
return 0;
}