-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.c
More file actions
62 lines (54 loc) · 1.04 KB
/
binarySearch.c
File metadata and controls
62 lines (54 loc) · 1.04 KB
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
59
60
61
62
#include<stdio.h>
int midPoint(int min,int max)
{
int mid;
mid = (min+max)/2;
return mid;
}
int binSearch(int a[],int key,int min,int max)
{
int mid;
if(max<min)
{
return -1; //If Array is empty
}
else
{
mid = midPoint(min,max);
if(a[mid]<key)
{
binSearch(a,key,min,mid-1);
}
else if(a[mid]>key)
{
binSearch(a,key,mid+1,max);
}
else
{
return mid;
}
}
}
int main()
{
int a[30],n,k,i,min,max,res;
printf("Enter the size of array::");
scanf("%d",&n);
printf("\nEnter the Sorted array::");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to be found::");
scanf("%d",&k);
res = binSearch(a,k,0,n-1);
if(res == -1)
{
printf("\nThe Value is not found");
}
else
{
printf("\nThe Value %d is at index %d",k,res);
}
return 0;
}