-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarysearch.cpp
More file actions
36 lines (28 loc) · 859 Bytes
/
Binarysearch.cpp
File metadata and controls
36 lines (28 loc) · 859 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
#include <iostream>
using namespace std;
int binarySearch(int arr[], int low, int up, int key) {
// Check base case
if (low > up)
return -1;// can not find
int mid = (up + low) / 2;
// Check if key is present at mid
if (arr[mid] == key)
return mid;
// If key greater, ignore left half
if (arr[mid] < key)
return binarySearch(arr, mid + 1, up, key);
// If key is smaller, ignore right half
return binarySearch(arr, low, mid - 1, key);
}
int main() {
int arr[] = {2, 3, 4, 10, 40, 51, 60, 73, 83, 99};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 73;
int result = binarySearch(arr, 0, n - 1 , key);
if (result == -1) {
cout << "Element is not in array!";
} else {
cout << "Element " << key << " is at index: " << result;
}
return 0;
}