forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imkaka_binary_search.cpp
55 lines (41 loc) · 1.07 KB
/
imkaka_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
53
54
55
/*
* @author : imkaka
* @date : 31/1/2019
*/
#include<iostream>
#include<cstdio>
using namespace std;
int binary_search_itr(int arr[], int size, int val){
int l = 0, r = size-1;
int mid = (l + r) / 2;
while(arr[mid] != val && l <= r){
if(val < arr[mid]){
r = mid - 1;
}
else{
l = mid +1;
}
mid = (l + r) / 2;
}
if(arr[mid] == val)
return mid;
return -1;
}
int binary_search_rec(int arr[], int left, int right, int val){
if(right >= left){
int mid = left + (right - left) / 2;
if(arr[mid] == val) return mid;
if(arr[mid] > val)
return binary_search_rec(arr, left, mid-1, val);
return binary_search_rec(arr, mid+1, right, val);
}
return -1;
}
int main(){
int arr[] = {1, 2, 3, 6, 9, 15, 16, 14};
int val = 9;
int size = sizeof(arr)/sizeof(arr[0]);
cout << "Iterative: " << binary_search_itr(arr, size, val) << endl;
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl;
return 0;
}