-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.cpp
79 lines (72 loc) · 2.58 KB
/
analyzer.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
#include <bits/stdc++.h>
#include "StringData.h"
using namespace std;
struct calculation
{
int index;
std::chrono::duration<double> totalTime;
};
typedef struct calculation structure;
structure calculateTime(std::function<int(vector<string>, string)> func, vector<string> container, string element)
{
structure toReturn;
auto time1 = std::chrono::system_clock::now();
toReturn.index = func(container, element);
auto time2 = std::chrono::system_clock::now();
toReturn.totalTime = time2 - time1;
return toReturn;
}
int linearSearch(vector<string>container, string element)
{
for (int i = 0; i < container.size(); i++)
{
if (container[i] == element)
return i;
}
return -1;
}
int binarySearch(vector<string> container, string element)
{
int min = 0;
int max = container.size() - 1;
int mid = (min + max) / 2;
while (container[mid] != element)
{
if (element > container[mid] and max > mid)
{
int tempmid = mid;
mid = int(ceil((double(mid) + double(max)) / 2));
int min = tempmid + 1;
}
else if (element < container[mid] and min < mid)
{
int tempmid = mid;
mid = (mid + min) / 2;
max = tempmid - 1;
}
else
{
return -1;
}
}
return mid;
}
int main()
{
vector<string> container = getStringData();
structure result;
result = calculateTime(linearSearch, container, "not_here");
cout << "Search for 'not_here' with Linear Search: i=" << result.index << " t=" << result.totalTime.count() << "s" << endl;
result = calculateTime(binarySearch, container, "not_here");
cout << "Search for 'not_here' with Binary Search: i=" << result.index << " t=" << result.totalTime.count() << "s" << endl;
result = calculateTime(linearSearch, container, "mzzzz");
cout << "Search for 'mzzzz' with Linear Search: i=" << result.index << " t=" << result.totalTime.count() << "s" << endl;
result = calculateTime(binarySearch, container, "mzzzz");
cout << "Search for 'mzzzz' with Binary Search: i=" << result.index << " t=" << result.totalTime.count() << "s" << endl;
result = calculateTime(linearSearch, container, "aaaaa");
cout << "Search for 'aaaaa' with Linear Search: i=" << result.index << " t=" << result.totalTime.count() << "s" << endl;
result = calculateTime(binarySearch, container, "aaaaa");
cout << "Search for 'aaaaa' with Binary Search: i=" << result.index << " t=" << result.totalTime.count() << "s" << endl;
return 0;
}
eoiofujosfuosfuoseu