-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbinary_search.c
More file actions
35 lines (32 loc) · 1021 Bytes
/
binary_search.c
File metadata and controls
35 lines (32 loc) · 1021 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
#include<stdio.h>
void main() {
int arraySize, searchElement, controlVar, controlBegin, controlMiddle, controlEnd, flag = 0;
printf("Enter the size of the array: ");
scanf("%d",&arraySize);
int array[arraySize];
printf("Enter the array elements(should be sorted in ascending order): \n");
for(controlVar = 0; controlVar < arraySize; controlVar++) {
scanf("%d",&array[controlVar]);
}
printf("Enter the element to be searched: ");
scanf("%d",&searchElement);
controlBegin = 0;
controlEnd = arraySize - 1;
while( (controlEnd >= controlBegin) ) {
controlMiddle = ( controlBegin + controlEnd ) / 2;
if(searchElement == array[controlMiddle]) {
printf("Element(%d)found at position %d\n",searchElement,controlMiddle);
flag = 1;
break;
}
else if(searchElement < array[controlMiddle]) {
controlEnd = controlMiddle - 1;
}
else if(searchElement > array[controlMiddle]) {
controlBegin = controlMiddle + 1;
}
}
if(flag = 0) {
printf("Element(%d) was not found\n",searchElement);
}
}