-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4sem_11.cpp
57 lines (42 loc) · 1.27 KB
/
4sem_11.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
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
int main() {
srand(time(0));
ofstream outfile("execution_times.csv6");
if (!outfile.is_open()) {
cerr << "Error opening file!" << endl;
return 1;
}
outfile << "Size,ExecutionTime" << endl;
for (int n = 1; n <= 10; ++n) {
int size = pow(2, n);
int* arr = new int[size];
for (int i = 0; i < size; ++i) {
arr[i] = rand() % 1000;
}
clock_t startTime = clock();
selectionSort(arr, size);
clock_t endTime = clock();
double executionTime = double(endTime - startTime) / CLOCKS_PER_SEC;
cout << "Size: " << size << ", Execution Time: " << executionTime << " seconds" << std::endl;
outfile << size << "," << executionTime << endl;
delete[] arr;
}
outfile.close();
return 0;
}