-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchOfArray.cpp
More file actions
55 lines (42 loc) · 1.39 KB
/
BinarySearchOfArray.cpp
File metadata and controls
55 lines (42 loc) · 1.39 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
#include <iostream>
#include <string>
using namespace std;
// Search array for existence of value using binary search.
// Each pass halves the searching space which gives the search
// O(log n) performance.
// Compiler: Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)
bool Exists(int key, int *a, const size_t size) {
int low = 0;
int high = size;
std::cout << std::endl << "Searching for " << key << std::endl;
// While there are still unexamined sections.
while (low <= high)
{
std:cout << " | " << low << " <--> " << high << " | " << std::endl;
int median = (low + high) / 2;
// Check if value at median is key
if (a[median] == key) {
std::cout << "FOUND at "<< median << std::endl;
return true;
}
// Search lower half?
if (a[median] > key)
high = median -1;
// Search upper half?
else
low = median + 1;
}
std::cout << "NOT FOUND" << std::endl;
return false;
}
int main() {
int values[] = {1,3,7,9,11,22,38,46,79,98,123,456,789,998};
const size_t arraySize = sizeof(values) / sizeof(values[0]);
auto exists = Exists(3, values, arraySize);
Exists(38, values, arraySize);
Exists(3, values, arraySize);
Exists(4, values, arraySize);
Exists(456, values, arraySize);
Exists(998, values, arraySize);
return 0;
}