forked from 790hanu/Annex-qr-code-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.c
More file actions
36 lines (31 loc) · 815 Bytes
/
BinarySearch.c
File metadata and controls
36 lines (31 loc) · 815 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
36
#include <stdio.h>
int main()
{
int i, first, last, middle, n, sv, array[20];
printf("Enter number of elements:");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (i=0;i<n;i++)
scanf("%4d", &array[i]);
printf("Enter value to find\n");
scanf("%d", &sv);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (array[middle] < sv)
first = middle + 1;
else if (array[middle] == sv)
{
printf("%d Found at location %d.\n", sv, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found\n", sv);
return 0;
}