-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_recursive.c
More file actions
58 lines (53 loc) · 910 Bytes
/
binary_search_recursive.c
File metadata and controls
58 lines (53 loc) · 910 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
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
int binary(int arr[], int value, int l, int r);
void main()
{
int n,i,j,temp,value;
printf("enter the value of n: ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//sort
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("sorted....\n");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("enter the value to be searched: ");
scanf("%d",&value);
printf("the value is at location %d", binary(arr,value,0,n-1));
}
int binary(int arr[], int value, int l, int r)
{ int mid;
if(l<=r)
{
mid=l+(r-1)/2;
if(arr[mid]==value)
{
return mid;
}
if(arr[mid]>value)
{
return binary(arr,value,l,mid-1);
}
if(arr[mid]<value)
{
return binary(arr,value,mid+1,r);
}
}
}