-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
56 lines (49 loc) · 1.56 KB
/
Copy pathbinarySearch.cpp
File metadata and controls
56 lines (49 loc) · 1.56 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
/*
Binary Search
*/
#include <iostream>
using std::cout;
int binarySearchRecursive(int arr[], int l, int r, int x);
int binarySearchImperative(int arr[], int l, int r, int x);
int main()
{
// a sorted collection
int arr[] = {1,2,3,5,8,13,21,34};
int x = 13; // we are searching for the index of this number
int n = sizeof(arr) / sizeof(arr[0]);
//int result = binarySearchRecursive(arr, 0, n-1, x);
int result = binarySearchImperative(arr, 0, n-1, x);
(result == -1 ) ? cout << x << " was not found.\n"
: cout << x << " was found at index " << result << '\n';
}
int binarySearchRecursive(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r-l)/2;
// is x at the midpoint itself?
if (arr[mid] == x)
return mid;
// if element less than mid, then look in left subarray
if (arr[mid] > x)
return binarySearchRecursive(arr, l, mid-1, x);
// else look in right subarray
return binarySearchRecursive(arr, mid+1, r, x);
}
// else return -1 if x not found
return -1;
}
int binarySearchImperative(int arr[], int l, int r, int x) {
while (r >= l) {
int mid = l + (r-l)/2;
// is x at the midpoint itself?
if (arr[mid] == x)
return mid;
// if x less than mid, then look in left half
if (arr[mid] > x)
r = mid - 1;
// else x is greater than mid, so look in right half
else
l = mid + 1;
}
// else return -1 if x not found
return -1;
}