-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
47 lines (34 loc) · 895 Bytes
/
binary_search.cpp
File metadata and controls
47 lines (34 loc) · 895 Bytes
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
#include <iostream>
#include <fstream>
using namespace std;
int binary_search( string s[], string, int num_items );
int main() {
fstream input;
input.open("strings.dat");
int num_items = 100;
string binary[num_items];
for( int i = 0; i < num_items && !input.eof(); i++ ) {
input >> binary[i];
}
input.close();
string search;
cout << "String to search for: ";
cin >> search;
int loc = binary_search(binary, search, num_items);
cout << "Binary Returned: " << binary[loc];
return 0;
}
int binary_search( string s[], string search, int num_items ) {
int lower_bound = 0;
int upper_bound = num_items -1;
while(upper_bound >= lower_bound) {
int mid = (lower_bound + upper_bound) / 2;
if( s[mid] == search ) {
return mid;
} else if( search < s[mid] ) {
upper_bound = mid + 1;
} else {
lower_bound = mid - 1;
}
}
}