-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path156Array.c
More file actions
56 lines (56 loc) · 996 Bytes
/
Copy path156Array.c
File metadata and controls
56 lines (56 loc) · 996 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
//Binary search program
#include<stdio.h>
#define size 5
void insert(int x[])
{
int i;
for(i=0;i<size;i++)
{
scanf("%d",&x[i]);
}
}
void display(int x[])
{
int i;
for(i=0;i<size;i++)
{
printf("%d\t",x[i]);
}
}
void b_search(int x[])
{
int b=0,e=size-1,fl=0,key,mid;
printf("\nEnter the value you want to search:\n");
scanf("%d",&key);
while(b<=e)
{
mid=(b+e)/2;
if(x[mid]==key)
{
fl=1;
break;
}
else if(key>=x[mid])
{
b=mid+1;
}
else
{
e=mid-1;
}
}
if(fl==0)
printf("\nNo such value found");
else
printf("\n%d found at %d location",key,(mid+1));
}
int main()
{
int a[size];
printf("\nEnter array elements:\n");
insert(a);
printf("\nThe array elements are:\n");
display(a);
b_search(a);
return 0;
}