-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-algo.cpp
52 lines (44 loc) · 1.13 KB
/
binary-search-algo.cpp
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
#include <iostream>
using namespace std;
int binSearch(int arr[], int first, int last, int target){
while (first <= last)
{
int middle = first + (last - first) / 2; // get middle index
if (arr[middle] == target) // if middle is equal to the target, it will adjust the middle indefinitely until the target match
{
return middle;
}
if (arr[middle] < target)
{
first = middle + 1;
}
else
{
last = middle - 1;
}
}
return 404;
}
int main(){
int size, target;
printf("Enter Size of the array: ");
cin >> size;
int arr[size];
cout << "Enter sorted " << size << " Elements!!!" << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
printf("Enter number you want to find: ");
cin >> target;
int findings = binSearch(arr, 0, size - 1, target);
if (findings == 404)
{
cout << "Element not found in the array";
}
else
{
cout << "Element found at index " << findings;
}
return 0;
}