-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
42 lines (37 loc) · 951 Bytes
/
binarySearch.cpp
File metadata and controls
42 lines (37 loc) · 951 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
40
41
42
#include <iostream>
using namespace std;
void printArray(int *arr, int size_of_array){
cout << "Array = {";
for(int i; i<size_of_array; i++){
cout<< arr[i] <<",";
if (i==size_of_array -1){
cout<<'\b';
}
}
cout <<"}\n";
}
int binarySearch(int *arr, int lowerBound, int upperBound, int val)
{
if (upperBound >= lowerBound) {
int mid = lowerBound + (upperBound - lowerBound) / 2;
if (arr[mid] == val)
return mid;
if (arr[mid] > val)
return binarySearch(arr, lowerBound, mid - 1, val);
return binarySearch(arr, mid + 1, upperBound, val);
}
return -1;
}
int main(){
int arr[] = {1,3,7,9,9,15,24};
int searching_val = 24;
int size= sizeof(arr)/sizeof(int);
printArray(arr, size);
int index = binarySearch(arr,0,size,searching_val);
if (index!=-1){
cout << "Value Found at index: " << index;
}
else {
cout <<"Value not found :(";
}
}