-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.cpp
More file actions
62 lines (58 loc) · 1.73 KB
/
analyzer.cpp
File metadata and controls
62 lines (58 loc) · 1.73 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
//
// Created by vli15 on 10/4/2024.
//
#include <iostream>
#include "analyzer.h"
#include <string>
#include <list>
#include <vector>
#include <chrono>
#include "StringData.h"
using namespace std;
int binary_search(vector<string>& container, string element) {
int middle = container.size()/2; //middle index
int high = container.size()-1;
int low = 0;
while(low <= high){
if (container[middle] == element) {
return middle;
}
else if( container[middle] < element) //the element is to the right of the middle value
{
low = middle + 1;
middle = (high + low)/2;
}
else //element is to the left of the middle value
{
high = middle - 1;
middle = (high + low)/2;
}
}
return -1;
}
int linear_search(vector<string>& container, string element) {
for(int i=0; i< container.size(); i++) {
if (container[i] == element)
return i;
}
return -1;
}
int main() {
string mylist[3] = { "mzzzz", "not_here", "aaaaa" };
vector<string> dataset = getStringData();
for (string word: mylist) {
auto starttimeL = chrono::system_clock::now() ;
int linear = linear_search(dataset, word);
auto endtimeL = chrono::system_clock::now();
cout<<"Linear time for "<<word<<" is: "<<endtimeL - starttimeL<<endl;
cout<<"Index: "<< linear<<endl;
auto starttimeB = chrono::system_clock::now();
int binary = binary_search(dataset, word);
auto endtimeB = chrono::system_clock::now();
cout<<"Binary time for "<< word<<" is: "<<endtimeB - starttimeB<<endl;
cout<<"Index: "<< binary<<endl;
cout<<endl;
}
// cout<<"hello world";
return 0;
}