-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAlgorithms.cpp
More file actions
91 lines (71 loc) · 1.74 KB
/
Copy pathSearchAlgorithms.cpp
File metadata and controls
91 lines (71 loc) · 1.74 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
#include <iostream>
#include <vector>
#include <chrono>
#include "SearchAlgorithms.h"
int main()
{
/*
* Grosses sortiertes Array erzeugen.
*/
std::vector<int> numbers;
for(int i = 0; i < 100000; i++)
{
numbers.push_back(i);
}
int target = 98765;
/*
* Zeitmessung fuer lineare Suche.
*/
auto startLinear =
std::chrono::high_resolution_clock::now();
int linearResult =
linearSearch(numbers, target);
auto endLinear =
std::chrono::high_resolution_clock::now();
auto linearTime =
std::chrono::duration_cast
<std::chrono::nanoseconds>
(endLinear - startLinear);
/*
* Zeitmessung fuer binaere Suche.
*/
auto startBinary =
std::chrono::high_resolution_clock::now();
int binaryResult =
binarySearch(numbers, target);
auto endBinary =
std::chrono::high_resolution_clock::now();
auto binaryTime =
std::chrono::duration_cast
<std::chrono::nanoseconds>
(endBinary - startBinary);
std::cout
<< "Gesuchtes Element: "
<< target
<< std::endl;
std::cout
<< "\nLineare Suche:"
<< std::endl;
std::cout
<< "Index = "
<< linearResult
<< std::endl;
std::cout
<< "Zeit = "
<< linearTime.count()
<< " ns"
<< std::endl;
std::cout
<< "\nBinaere Suche:"
<< std::endl;
std::cout
<< "Index = "
<< binaryResult
<< std::endl;
std::cout
<< "Zeit = "
<< binaryTime.count()
<< " ns"
<< std::endl;
return 0;
}