forked from itsprueba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinarySearch.c
More file actions
34 lines (33 loc) · 808 Bytes
/
binarySearch.c
File metadata and controls
34 lines (33 loc) · 808 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
#include<conio.h>
#include <stdio.h>
void search();
int arr[100], n, number, loc, beg, mid, end, i;
int main(){
printf("\nEnter the size of array: ");
scanf("%d", &n);
printf("\nEnter ordered elements:\n");
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
printf("\nEnter number to be searchedin the console: ");
scanf("%d", &number);
search();
getchar();
return 0;
}
void search(){
beg = 0;
mid = (beg + end) / 2;
end = n-1;
while ((beg<=end) && (arr[mid]!=number)){
if (number < arr[mid])
end = mid - 1;
else
beg = mid + 1;
mid = (beg + end) / 2;
}
if (arr[mid] == number)
printf("\n\nNumber found at location %d", mid+1);
else
printf("\n\nNumber not found in array");
}