forked from GDSC-UPES/Git-Github-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.c
More file actions
29 lines (24 loc) · 758 Bytes
/
recursion.c
File metadata and controls
29 lines (24 loc) · 758 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
#include <stdio.h>
int recursive_binary_search(int arr[], int l, int r, int value) {
int mid = (l+r)/2;
if(value == arr[mid])
return mid;
if ((value > arr[l]) && (value < arr[mid]))
return recursive_binary_search(arr, l, mid-1, value);
else if ((value > arr[mid]) && (value < arr[r]))
return recursive_binary_search(arr, mid+1, r, value);
}
int main() {
int n, f;
printf("Enter size of the array \n");
scanf("%d", &n);
int arr[n];
for(int i=0; i<n; i++) {
printf("Enter Element %d of the array \n", i+1);
scanf("%d", &arr[i]);
}
printf("Enter the element to be find \n");
scanf("%d", &f);
int x = recursive_binary_search(arr, 0, n-1, f);
printf("%d", x);
}