-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch(IterativeAndRecursive).cpp
More file actions
78 lines (66 loc) · 2.75 KB
/
BinarySearch(IterativeAndRecursive).cpp
File metadata and controls
78 lines (66 loc) · 2.75 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
#include <iostream>
using namespace std;
/*This program shows the implementation of both the recursive binary search function as well as iterative binary search.
User enters an array of 10 numbers in ascending order.
Binary Search (both recursive and iterative) returns the location of the key if present, otherwise returns -1 */
int recursiveBinarySearch(int arr[], int left, int right, int key)
{
if (left<=right) {
int mid = left + (right - left) / 2;
// If element to search is equal to the current middle element, return it
if (arr[mid] == key)
return mid;
// If element to search is smaller than middle value, then it can only be present in left subarray
if (arr[mid] > key)
return recursiveBinarySearch(arr, left, mid - 1, key);
// If element to search is larger than middle value, then it can only be present in right subarray
return recursiveBinarySearch(arr, mid + 1, right, key);
}
//Element is not found in the array
return -1;
}
int iterativeBinarySearch(int arr[], int left, int right, int key)
{
while (left <= right) {
int mid = left + (right - left) / 2;
// If element to search is equal to the current middle element, return it
if (arr[mid] == key) {
cout << "Middle value is now " << arr[mid] << " . Element found!" <<endl;
return mid;
}
// If element to search is smaller than middle value, then it can only be present in left subarray
if (arr[mid] >key) {
cout << "Middle value is now " << arr[mid] << endl;
cout << "have to search the left half now" <<endl;
cout << endl;
right = mid - 1;
}
// If element to search is larger than middle value, then it can only be present in right subarray
else {
cout << "Middle value is now " << arr[mid] << endl;
cout << "have to search the right half now" <<endl;
cout << endl;
left = mid + 1;
}
}
//Element is not found in the array
return -1;
}
int main() {
int size = 10;
int arr[size];
cout << "Enter a list of 10 numbers in ascending order to perform Binary Search : ";
for (int i=0; i<10; i++){
cin >> arr[i];
}
cout << endl;
int key;
cout << "Please enter a number from the list to search: " << endl;
cin >> key;
cout << "Displaying the Working of Binary Search: " <<endl;
int res = iterativeBinarySearch(arr, 0, size-1, key);
int res2 = recursiveBinarySearch(arr, 0, size-1, key);
(res == -1)||(res2==-1) ? cout << "Element not found in array"
: cout << "Element found at index " << res+1;
return 0;
}