-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary-Search.cpp
71 lines (50 loc) · 1.53 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Including header file for using common functionalities
#include <iostream>
//Declaring function defination, to let
//compiler know that this function do exists
int Binary_Search(int arr[], int size, int key);
int rec_Binary_Search(int arr[], int left, int right, int key);
int main(){
int arr[50], n, key;
std::cout<< "Enter the size of your array : " ;
std::cin >> n;
std::cout<< "Enter the elements of your array : " ;
for(int i=0; i<n; ++i)
std::cin >>arr[i];
std::cout<< "Enter the value to be search : ";
std::cin >> key;
int result = Binary_Search(arr,n,key);
if(result == -1)
std::cout<< key, " not exist!";
else
std::cout<< key << " found at index " << result;
return 0;
}
//Iterative approach for Binary-Search
int BinarySearch(int arr[], int size, int key){
int left = 0;
int right = size;
while (left < right){
int mid = (left + right) / 2;
if(arr[mid] == key)
return key;
else if(arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
//Recursive approach for Binary-Search
int rec_Binary_Search(int arr[], int left, int right, int key){
if(left <= right){
int mid = (left + right)/2;
if(arr[mid] == key)
return mid;
else if (arr[mid] > key)
return rec_Binary_Search(arr,left, mid-1,key);
else
return rec_Binary_Search(arr,mid+1,right, key);
}
return -1;
}