-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path05_recursion_binary_search.cpp
More file actions
90 lines (75 loc) · 1.46 KB
/
05_recursion_binary_search.cpp
File metadata and controls
90 lines (75 loc) · 1.46 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Topic - Time to Try Recursion!
Write a Recursive Functions to perform binary search.
Eg: arr = {1, 2, 3, 4, 5, 10, 20}
key = 10
*/
#include <iostream>
using namespace std;
int binarySearch(int *a, int start, int end, int key)
{
int mid = start + (end-start)/2;
// base case
if(start>end)
{
return -1;
}
// recursive case
if(a[mid]==key)
{
return mid;
}
else if (key < a[mid])
{
return binarySearch(a, start, mid-1,key);
}
else
{
return binarySearch(a, mid+1, end,key);
}
}
// function to drive code
int main()
{
int size;
cout << "Enter array size: ";
cin >> size;
int arr[size];
cout << "Enter Elements: ";
for(int i=0; i<size; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter Search Key: ";
cin >> key;
// search for value
int idx = binarySearch(arr,0 ,size-1, key);
// display output
if(idx >= 0){
cout << "Value found at index: " << idx;
}
else{
cout << "Value not found: ";
}
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter array size: 5
Enter Elements: 1 3 4 5 15
Enter Search Key: 5
Value found at index: 3
Case 2:
Enter array size: 5
Enter Elements: 1 3 4 5 15
Enter Search Key: 2
Value not found
Case 3:
Enter array size: 5
Enter Elements: 1 3 4 5 15
Enter Search Key: -10
Value not found
*/