forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.cpp
29 lines (28 loc) · 897 Bytes
/
BinarySearch.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
#include <iostream>
int binarySearch(int arr[], int p, int r, int num) {
if (p <= r) {
int mid = (p + r) / 2;
if (arr[mid] == num)
return mid;
if (arr[mid] > num)
return binarySearch(arr, p, mid - 1, num);
if (arr[mid] < num)
return binarySearch(arr, mid + 1, r, num);
}
return -1;
}
int main() {
int arr[] = { 1, 3, 7, 15, 18, 20, 25, 33, 36, 40 };//hardcoding the array
int n = sizeof(arr) / sizeof(arr[0]);//size of entered array
int num;
std::cout << "Enter the number to search: \n";
std::cin >> num;
int index = binarySearch(arr, 0, n - 1, num);//function call and storing the value to index
if (index == -1) {
std::cout << num << " doesn't exist in the array";
}
else {
std::cout << num << " is at index " << index << " in the array";
}
return 0;
}