-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Search_using_recursion.c
46 lines (40 loc) · 1.14 KB
/
Binary_Search_using_recursion.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
// Q. Write a program to implement Binary Search using recursion
#include <stdio.h>
int binarySearch ( int [], int, int, int );
int main()
{
int n = 5, result, target;
int arr[n] = { 7, 18, 29, 43, 99 };
printf("Elements in the array : ");
for( int i = 0; i < 5; ++i )
printf("%d "arr[i]);
printf("\n");
// Mid case
target = 29;
result = binarySearch ( arr, 0, n-1, target );
printf("Element %d is at %d index",target, result);
target = 7;
result = binarySearch ( arr, 0, n-1, target );
printf("%d",result);
target = 43;
result = binarySearch ( arr, 0, n-1, target );
printf("%d",result);
target = 100;
result = binarySearch ( arr, 0, n-1, target );
printf("%d",result);
return 0;
}
int binarySearch ( int arr [], int start, int end, int target )
{
if( start <= end )
{
int mid = start+(end-start)/2;
if( arr[mid] == target )
return mid;
else if ( arr[mid] > target )
return binarySearch( arr, start, mid-1, target );
else
return binarySearch( arr, mid+1, end, target);
}
return -1;
}