-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_rec.c
90 lines (82 loc) · 1.15 KB
/
bin_rec.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
void print_arr(int *arr,int size)
{
int i;
printf("Index\t:\tPosition ");
printf("\n");
for(i=0;i<size;i++)
{
printf("[%d]\t:\t%d\n",i,arr[i]);
}
printf("\n");
}
void scan_arr(int *arr,int n)
{
int i;
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
print_arr(arr,n);
}
int bin_search(int *arr,int low,int high,int ele)
{
int mid;
mid=(low+high)/2;
if(low>high)
{
return -1;
}
else if(arr[mid]==ele)
{
return mid;
}
else if(arr[mid]<ele)
{
low=mid+1;
bin_search(arr,low,high,ele);
}
else
{
high=mid-1;
bin_search(arr,low,high,ele);
}
}
void main()
{
int arr[50],n,low,high,ele,a;
printf("Enter the no of elements : ");
scanf("%d",&n);
scan_arr(arr,n);
printf("Enter the element to be searched : ");
scanf("%d",&ele);
low=0;
high=n-1;
a = bin_search(arr,low,high,ele);
if(0<=a)
{
printf("Element found at index : %d\n",a);
}
else
{
printf("Element not found!!\n");
}
}
/*
Enter the no of elements : 5
Enter the elements:
1
2
3
4
5
Index : Position
[0] : 1
[1] : 2
[2] : 3
[3] : 4
[4] : 5
Enter the element to be searched : 4
Element found at index : 3
*/