forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_search.cpp
52 lines (43 loc) · 1.09 KB
/
Binary_search.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 binarySearch(int arr[], int low, int high, int key)
{
if (high >= low) {
int mid = low + (high - low) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == key)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > key)
return binarySearch(arr, low, mid - 1, key);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, key);
}
// We reach here when element is not
// present in array
return -1;
}
int main()
{
int n, key, result = 0;
cout << "Enter the size of the array " << endl;
cin >> n;
int array[n];
cout << "Enter the elements of the array" << endl;
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
cout << "Enter the element to be searched" << endl;
cin >> key;
result = binarySearch(array, 0, n - 1, key);
if (result == -1) {
cout << "Element " << key << " not found in the array" << endl;
} else {
cout << "Element " << key << " found at the index " << result << endl;
}
return 0;
}