-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.cpp
More file actions
90 lines (73 loc) · 3.07 KB
/
Copy pathanalyzer.cpp
File metadata and controls
90 lines (73 loc) · 3.07 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include "StringData.h"
using namespace std;
using namespace std::chrono;
int linear_search(const vector<string>& container, const string& element) {
for(int i = 0; i < container.size(); i++)
{
if (container[i] == element)
return i;
}
return -1;
}
int binary_search(const vector<string>& container, const string& element) {
int low = 0;
int high = container.size() - 1;
int middle = (low + high) / 2;
while(low <= high) {
int middle = low + (high - low) / 2;
if(container[middle] == element) {
return middle;
}
else if(container[middle] < element) {
low = middle + 1;
}
else {
high = middle - 1;
}
}
return -1;
}
int main()
{
vector<string>data = getStringData();
cout << "Time difference between linear search and binary search" << endl;
cout << "*******************************************************" << endl;
auto start = chrono::system_clock::now();
int index = linear_search(data, "not_here");
auto end = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = end - start;
cout << "Linear search for 'not_here': index " << index << " and takes " << elapsed_seconds.count() << " seconds\n";
// Measure time for binary search (for "not_here")
auto start2 = chrono::system_clock::now();
int index2 = binary_search(data, "not_here");
auto end2 = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds2 = end2 - start2;
cout << "Binary search for 'not_here': index " << index2 << " and takes " << elapsed_seconds2.count() << " seconds\n";
cout << endl;
// Repeat for other searches (e.g., "mzzzz")
auto start3 = chrono::system_clock::now();
int index3 = linear_search(data, "mzzzz");
auto end3 = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds3 = end3 - start3;
cout << "Linear search for 'mzzzz': index " << index3 << " and takes " << elapsed_seconds3.count() << " seconds\n";
auto start4 = chrono::system_clock::now();
int index4 = binary_search(data, "mzzzz");
auto end4 = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds4 = end4 - start4;
cout << "Binary search for 'mzzzz': index " << index4 << " and takes " << elapsed_seconds4.count() << " seconds\n";
cout << endl;
auto start5 = chrono::system_clock::now();
int index5 = linear_search(data, "aaaaa");
auto end5 = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds5 = end4 - start4;
cout << "Linear search for 'aaaaa': index " << index5 << " and takes " << elapsed_seconds5.count() << " seconds\n";
auto start6 = chrono::system_clock::now();
int index6 = binary_search(data, "aaaaa");
auto end6 = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds6 = end6 - start6;
cout << "Binary search for 'aaaaa': index " << index6 << " and takes " << elapsed_seconds6.count() << " seconds\n";
}