-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.c
More file actions
executable file
·53 lines (48 loc) · 882 Bytes
/
binary.c
File metadata and controls
executable file
·53 lines (48 loc) · 882 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
/*binary search method divides the given array exactly into two halves each
time reducing the search array to half, thus it is efficient searching method
for sorted array */
#include<stdio.h>
int binary(int a[],int k,int l,int h)
{
if(l==h)
{
if(k==a[l])
return l;
else
return -1;
}
else
{
int mid=(l+h)/2;
if(k==a[mid])
return mid;
else
{
if(k<a[mid])
binary(a,k,l,mid-1);
else
binary(a,k,mid+1,h);
}
}
}
int main(int argc, char* argv[]) /* takes arguments as (program key search_file)*/
{
int val;
FILE *input;
int key = atoi(argv[1]);
input=fopen(argv[2],"r+");
int a[101],count=0;
while(fscanf(input,"%d\t",&val)!=EOF)
{
a[count++]=val;
}
fclose(input);
int l=0;
int h=count-1;
int result=binary(a,key,l,h);
if(result==-1)
printf("Key not found in the file....Try again !!\n");
else
printf("Index Position of %d is %d\n",key,result+1);
return 0;
}