-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4sem_35.cpp
83 lines (73 loc) · 2 KB
/
4sem_35.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
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int pivot_swap(int n, int arr[], ofstream &outfile) {
int pivot = arr[0];
int i = 1;
int j = n;
clock_t start_time = clock();
while (i <= j) {
while (i <= n - 1 && arr[i] < pivot) {
i++;
}
while (j > 0 && arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr[i], arr[j]);
i++;
j--;
}
}
swap(arr[0], arr[j]);
clock_t end_time = clock();
double execution_time = double(end_time - start_time) / CLOCKS_PER_SEC;
outfile << "Pivot Swap," << "," << "," << execution_time << endl; // Write time for pivot swap
return j;
}
int search(int arr[], int n, int k, ofstream &outfile) {
int index;
clock_t start_time, end_time;
while (true) {
start_time = clock();
index = pivot_swap(n, arr, outfile);
end_time = clock();
double execution_time = double(end_time - start_time) / CLOCKS_PER_SEC;
outfile << "Pivot Swap," << "," << "," << execution_time << endl; // Write time for pivot swap
if (index == k - 1) {
return arr[index];
} else if (index > k - 1) {
n = index;
} else {
arr += index + 1;
n -= index + 1;
k -= index + 1;
}
}
}
int main() {
srand(time(0));
const int n = 100;
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
}
ofstream outfile("search_ele_execution_times.csv");
if (!outfile.is_open()) {
cerr << "Error opening file!" << endl;
return 1;
}
outfile << "Step,Low,High,ExecutionTime" << endl;
int k;
cout << "Enter the value of k: ";
cin >> k;
search(arr, n, k, outfile);
outfile.close();
return 0;
}