-
Notifications
You must be signed in to change notification settings - Fork 460
/
recBinSearch.c
37 lines (37 loc) · 913 Bytes
/
recBinSearch.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
// Binary Search - Recursive Approach
#include <stdio.h>
void readArr(int[],int);
int recBinSearch(int[],int,int,int);
void main()
{
int A[20],n,key,x;
printf("Enter the number of elements: ");
scanf("%d",&n);
readArr(A,n);
printf("Enter the element to search: ");
scanf("%d",&key);
x=recBinSearch(A,0,n-1,key);
if(x==-1)
printf("Element not found");
else
printf("Element found at position %d",x+1);
}
void readArr(int A[],int n)
{
int i;
printf("Enter %d number of elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
}
int recBinSearch(int A[],int lwr,int upr,int key)
{
int mid=(lwr+upr)/2;
if(lwr>upr)
return -1;
else if(A[mid]==key)
return mid;
else if(A[mid]>key)
return recBinSearch(A,lwr,mid-1,key);
else
return recBinSearch(A,mid+1,upr,key);
}