-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path10.c
More file actions
56 lines (45 loc) · 960 Bytes
/
Copy path10.c
File metadata and controls
56 lines (45 loc) · 960 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
int main()
{
int i, key, size, low, high, mid, pos;
printf("Enter the number of elements: ");
scanf("%d", &size);
int elements[size];
printf("Enter %d integers in ascending order:\n", size);
for (i = 0; i < size; i++)
{
scanf("%d", &elements[i]);
}
printf("Enter the search key: ");
scanf("%d", &key);
low = 0;
high = size - 1;
mid = (low + high) / 2;
pos = -1;
while (low <= high)
{
if (elements[mid] == key)
{
pos = mid;
break;
}
else if (elements[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
mid = (low + high) / 2;
}
if (pos != -1)
{
printf("%d found at location %d.\n", key, pos + 1);
}
else
{
printf("Element %d is not present in the array.\n", key);
}
return 0;
}