Skip to content

Commit 7ec3108

Browse files
authored
Merge pull request deutranium#78 from swapnil-satpathy/master
Added code for Binary Search from Scratch in C
2 parents 2fa8452 + 83c75f1 commit 7ec3108

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
#include <stdio.h>
2+
3+
4+
5+
// The below function module implements binary_search functionality in an iterative manner.
6+
// If the element is found then it returns the index otherwise -1;
7+
int binary_search(int arr[],int l,int r,int x){
8+
while(l<=r){
9+
10+
// This way of calculating mid removes the chances of overflow with respect to the size of integer.
11+
int mid=l+(r-l)/2;
12+
if(arr[mid] == x)
13+
return mid;
14+
else if(arr[mid]<x)
15+
l=mid+1;
16+
else
17+
r=mid-1;
18+
19+
20+
}
21+
return -1;
22+
}
23+
24+
/*
25+
26+
The below functionality finds the lower_bound of an element in an array.
27+
28+
Lower_bound returns the index of first occurrence of an element if the element is present in the array.
29+
If the element is not present in the array, then returns the index of element just greater than the given element.
30+
If the index returned is same as the length of the array,
31+
then it means the element given is greater than all the elements in the array
32+
33+
*/
34+
35+
36+
int lower_bound(int arr[],int l,int r,int x){
37+
while(l<=r){
38+
int mid=l+(r-l)/2;
39+
40+
if(arr[mid]>=x)
41+
r=mid;
42+
else
43+
l=mid+1;
44+
if(l==r)
45+
break;
46+
}
47+
return l;
48+
49+
}
50+
51+
52+
/*
53+
The below functionality finds the upper_bound of an element in an array.
54+
55+
Upper_bound returns the index of the next greater element present than the element x passed as in input.
56+
If the output is the length of the array, it means that the upper_bound element is not present in the array
57+
*/
58+
59+
int upper_bound(int arr[],int l,int r,int x){
60+
while(l<=r){
61+
int mid=l+(r-l)/2;
62+
63+
if(arr[mid]<=x)
64+
l=mid+1;
65+
else
66+
r=mid;
67+
if(l==r)
68+
break;
69+
}
70+
return l;
71+
72+
}
73+
74+
/*
75+
The below functionality implements floor functionality, i.e
76+
it returns the array element less than or equal to the input x.
77+
*/
78+
79+
80+
int floor_(int arr[],int l,int r,int x){
81+
int res=-1;
82+
while(l<=r){
83+
int mid=l+(r-l)/2;
84+
if(arr[mid]==x)
85+
return mid;
86+
else if(arr[mid]<x){
87+
l=mid+1;
88+
res=mid;
89+
}
90+
else{
91+
r=mid-1;
92+
93+
}
94+
95+
}
96+
return res;
97+
}
98+
99+
100+
/*
101+
The below functionality implements ceil functionality, i.e
102+
it returns the array element greater than or equal to the input x.
103+
*/
104+
105+
106+
int ceil_(int arr[],int l,int r,int x){
107+
int res=-1;
108+
while(l<=r){
109+
int mid=l+(r-l)/2;
110+
if(arr[mid]==x)
111+
return mid;
112+
else if(arr[mid]>x){
113+
r=mid-1;
114+
res=mid;
115+
}
116+
else{
117+
l=mid+1;
118+
119+
}
120+
121+
}
122+
return res;
123+
}
124+
125+
126+
127+
int main(){
128+
129+
printf("Enter the number of elements in the array\n");
130+
int n;
131+
scanf("%d",&n);
132+
133+
int arr[n];
134+
135+
int a;
136+
printf("Enter the elements of the array in sorted manner\n");
137+
for(int i=0;i<n;i++){
138+
scanf("%d",&a);
139+
arr[i]=a;
140+
}
141+
int choice;
142+
int x;
143+
144+
145+
while(1){
146+
printf("Please enter the number to be operated upon(ex. to be searched)\n");
147+
148+
scanf("%d",&x);
149+
150+
151+
printf("Please enter the functionality you want to be operated upon\n");
152+
printf("1 for binary_search\n");
153+
printf("2 for lower_bound\n");
154+
printf("3 for upper_bound\n");
155+
printf("4 for ceil\n");
156+
printf("5 for floor\n");
157+
printf("6 to exit\n");
158+
scanf("%d",&choice);
159+
160+
161+
if(choice == 1){
162+
163+
int index=binary_search(arr,0,n-1,x);
164+
if(index == -1)
165+
printf("The element %d is not present in the array\n", x);
166+
else
167+
printf("The element %d is present at index %d of the array\n", x,index);
168+
169+
printf("*************************************************\n");
170+
printf("\n");
171+
172+
}
173+
else if(choice == 2){
174+
int index=lower_bound(arr,0,n-1,x);
175+
176+
printf("The Lower_bound of the element %d in the array is %d\n", x,index);
177+
printf("*************************************************\n");
178+
printf("\n");
179+
180+
}
181+
else if(choice == 3){
182+
int index=upper_bound(arr,0,n-1,x);
183+
printf("The Upper_bound of the element %d in the array is %d\n", x,index);
184+
printf("*************************************************\n");
185+
printf("\n");
186+
187+
}
188+
else if(choice == 4){
189+
int index=ceil_(arr,0,n-1,x);
190+
if(index == -1 || index==n)
191+
printf("The ceil of the array is not present in the array\n");
192+
193+
else
194+
printf("The ceil of the element %d in the array is %d\n", x,arr[index]);
195+
printf("*************************************************\n");
196+
printf("\n");
197+
}
198+
else if(choice == 5){
199+
int index=floor_(arr,0,n-1,x);
200+
201+
if(index == -1)
202+
printf("The floor of the element is not present in the array\n");
203+
else
204+
printf("The floor of the element %d in the array is %d\n", x,arr[index]);
205+
printf("*************************************************\n");
206+
printf("\n");
207+
}
208+
209+
else if(choice == 6){
210+
printf("Bubyee\n");
211+
break;
212+
}
213+
else{
214+
printf("Please provide a valid choice\n");
215+
}
216+
}
217+
218+
return 0;
219+
}
220+

0 commit comments

Comments
 (0)