-
Notifications
You must be signed in to change notification settings - Fork 0
/
binay_recursive.c
55 lines (42 loc) · 1.41 KB
/
binay_recursive.c
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
#include <stdio.h>
// Function to perform recursive binary search
int binarySearch(int arr[], int left, int right, int target) {
if (right >= left) {
int mid = left + (right - left) / 2;
// Check if the target is present at mid
if (arr[mid] == target) {
return mid; // Target found at index mid
}
// If target is smaller, search in the left half
if (arr[mid] > target) {
return binarySearch(arr, left, mid - 1, target);
}
// Otherwise, search in the right half
return binarySearch(arr, mid + 1, right, target);
}
return -1; // Target is not present in the array
}
int main() {
int n, target;
// Input number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input elements of the array
printf("Enter %d sorted elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the target value to search
printf("Enter the value to search: ");
scanf("%d", &target);
// Perform binary search
int result = binarySearch(arr, 0, n - 1, target);
// Output the result
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;
}