-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchingbasic.cpp
More file actions
39 lines (36 loc) · 909 Bytes
/
searchingbasic.cpp
File metadata and controls
39 lines (36 loc) · 909 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<bits/stdc++.h>
using namespace std;
int binarysearch(int arr[], int n, int ele){
int low = 0, high = n - 1;
while(low <= high){
int mid = (low + high) / 2;
if(ele == arr[mid]){
return mid;
}
else if(ele < arr[mid]){
high = mid - 1;
}else{
low = mid + 1;
}
}
return -1;
}
int recursive_binarysearch(int arr[], int low, int high, int ele){
int mid = (low + high) / 2;
if(low > high){
return -1;
}
if(arr[mid] == ele){
return mid;
}else if(ele < arr[mid]){
return recursive_binarysearch(arr, low, mid - 1, ele);
}else{
return recursive_binarysearch(arr, mid + 1, high, ele);
}
}
int main(){
int arr[] = {10, 15, 20, 25, 30, 35};
// cout << binarysearch(arr, 2, 5);
cout << recursive_binarysearch(arr, 0, 5, 30);
return 0;
}