-
Notifications
You must be signed in to change notification settings - Fork 8
/
BSERREC.C
39 lines (36 loc) · 806 Bytes
/
BSERREC.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
#include<stdio.h>
#include<conio.h>
int search(int [],int,int);
int binsearch(int [],int,int,int);
void main()
{
int a[10],i,n,x,loc;
clrscr();
printf("Enter the size : ");
scanf("%d",&n);
printf("\nEnter %d elements into the array in ascending order:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&x);
loc=search(a,n,x);
if(loc==-1) printf("\n%d is not found.",x);
else printf("\n%d is found at position %d in the array.",x,loc+1);
getch();
}
int search(int a[],int n,int v)
{
return binsearch(a,0,n-1,v);
}
int binsearch(int a[],int lb,int ub,int v)
{
int middle=(lb+ub)/2;
if(lb>ub) return -1;
else if(a[middle]==v) return middle;
else
{
if(v<a[middle]) ub=middle-1;
else lb=middle+1;
return binsearch(a,lb,ub,v);
}
}