-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.cpp
More file actions
100 lines (88 loc) · 3.16 KB
/
analyzer.cpp
File metadata and controls
100 lines (88 loc) · 3.16 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
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cmath>
#include "StringData.h"
using namespace std;
int linear_search(std::vector<std::string> container, std::string element){
for(int x=0; x < sizeof(container); x++){
if(container[x]==element){
return x;
}
}
return -1;
}
int binary_search(std::vector<std::string> container, std::string element){
int minIndex = 0;
int maxIndex = sizeof(container);
int midIndex;
while(minIndex<=maxIndex){
midIndex = minIndex + floor((maxIndex - minIndex) / 2);
if (container[midIndex] == element){
return midIndex;
}
else if (container[midIndex] > element) {
maxIndex = midIndex - 1;
}
else if (container[midIndex] < element){
minIndex = midIndex + 1;
}
}
return -1;
}
int main() {
std::vector<std::string> data = getStringData();
cout << "Linear Search for 'not_here': \n";
long initialTime = systemTimeNanoseconds()/1000000000;
cout << "The index of the linear search is:";
int LinearSearch1 = linear_search(data, "not_here");
cout <<LinearSearch1;
long finalTime = systemTimeNanoseconds()/1000000000;
long timeElapsed = initialTime-finalTime;
cout << "\nTime elapsed:";
cout << timeElapsed;
cout << "\nBinary Search for 'not_here':";
initialTime = systemTimeNanoseconds()/1000000000;
cout << "\nThe index of the binary search is:";
int BinarySearch1 = binary_search(data, "not_here");
cout <<BinarySearch1;
finalTime = systemTimeNanoseconds()/1000000000;
timeElapsed = initialTime-finalTime;
cout << "\nTime elapsed:";
cout << timeElapsed;
cout << "\n\nLinear Search for 'mzzzz': \n";
initialTime = systemTimeNanoseconds()/1000000000;
cout << "The index of the linear search is:";
int LinearSearch2 = linear_search(data, "mzzzz");
cout <<LinearSearch2;
finalTime = systemTimeNanoseconds()/1000000000;
timeElapsed = initialTime-finalTime;
cout << "\nTime elapsed:";
cout << timeElapsed;
cout << "\nBinary Search for 'mzzzz':";
initialTime = systemTimeNanoseconds()/1000000000;
cout << "\nThe index of the binary search is:";
int BinarySearch2 = binary_search(data, "mzzzz");
cout <<BinarySearch2;
finalTime = systemTimeNanoseconds()/1000000000;
timeElapsed = initialTime-finalTime;
cout << "\nTime elapsed:";
cout << timeElapsed;
cout << "\n\nLinear Search for 'aaaaa': \n";
initialTime = systemTimeNanoseconds()/1000000000;
cout << "The index of the linear search is:";
LinearSearch1 = linear_search(data, "aaaaa");
cout <<LinearSearch1;
finalTime = systemTimeNanoseconds()/1000000000;
timeElapsed = initialTime-finalTime;
cout << "\nTime elapsed:";
cout << timeElapsed;
cout << "\nBinary Search for 'not_here':";
initialTime = systemTimeNanoseconds()/1000000000;
cout << "\nThe index of the binary search is:";
BinarySearch1 = binary_search(data, "aaaaa");
cout <<BinarySearch1;
finalTime = systemTimeNanoseconds()/1000000000;
timeElapsed = initialTime-finalTime;
cout << "\nTime elapsed:";
cout << timeElapsed;
return 0;
}