-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
49 lines (37 loc) · 846 Bytes
/
binary_search.cpp
File metadata and controls
49 lines (37 loc) · 846 Bytes
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
#include "stdio.h"
// recursive
int bsearch_rec(int sorted_arr[], int start, int end, int key) {
if (end >= start)
{
int mid = start + (end-start)/2;
//printf("%d\n", mid);
if (sorted_arr[mid] == key) {
return mid;
}
else if(sorted_arr[mid] > key) {
return bsearch (sorted_arr, start, mid-1, key);
}
else {
return bsearch (sorted_arr, mid+1, end, key);
}
}
return -1;
}
// iterative
int bsearch_it(int arr[], int start, int end, int key) {
while (start <= end) {
int mid = start + (end-start)/2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
start = mid + 1;
else
end = mid - 1;
}
return -1;
}
int main() {
int sorted_arr[] = {2, 3, 5, 7, 9, 10, 15, 19};
printf("%d\n", bsearch_it(sorted_arr, 0, 7, 10));
return 0;
}