-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
39 lines (37 loc) · 788 Bytes
/
BinarySearch.cpp
File metadata and controls
39 lines (37 loc) · 788 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
#include <iostream>
using namespace std;
int BinarySearch(int arr[], int size, int element)
{
int low, mid, high; // declared high mid and low example in array arr[]={1,2,3,4,5,6,7}
// 1 is low,7 is high and 3 is mid(due to int)
low = 0;
high = arr[size - 1]; //
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] == element)
{
return mid;
}
if (arr[mid] < element)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int arr[] = {8, 4, 11, 32};
int n;
cout << "Enter element to search:";
cin >> n;
int size = sizeof(arr) / sizeof(int);
int searchIndex = BinarySearch(arr, size, n);
cout << "The element is " << n << " and position is " << searchIndex;
return 0;
}