Repository navigation
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
77 lines (58 loc) · 1.42 KB
/
Copy pathbinarySearch.cpp
File metadata and controls
77 lines (58 loc) · 1.42 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
#include <iostream>
using namespace std;
int COMPARE(int a, int b)
{
if(a > b) return 1;
else if (a < b) return -1;
else return 0;
}
int binarySearch(int list[], int searchnum, int left, int right)
{
int middle, val; //index
while(left<=right){
middle = (left+right)/2;
val = COMPARE(list[middle],searchnum);
switch(val)
{
case -1 : left = middle + 1; break;
case 0 : return middle;
case 1: right = middle -1;
}
}
return -1;
}
int main()
{
int n, searchnum, left, right;
cout << "How many integers: ";
cin >> n;
if(n<=0) {
cout << "Invalid list size.\n";
return 1;
}
int list[100];
if(n>100){
cout << "Maximum 100 integers allowed.\n";
return 1;
}
cout << "Enter " << n
<< " integers in ascending order: ";
for (int i = 0; i < n; i++){
if (! (cin >> list[i])) {
cout << "Invalid input. Must be an integer!!!";
return 1;
}
}
cout << "Enter the number you want to search for: ";
//cin >> searchnum;
if (!(cin >> searchnum)) {
cout << "Invalid integer input.\n";
return 1;
}
int result = binarySearch(list, searchnum, 0, n - 1);
if (result != -1)
cout << "Found at index " << result << '\n';
else
cout << "Number not found.";
return 0;
}