-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch_binary.c
More file actions
41 lines (34 loc) · 788 Bytes
/
search_binary.c
File metadata and controls
41 lines (34 loc) · 788 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
// [binary search]
/*
#include <stdio.h>
int main()
{
int a[10], i, n, first, last, mid, search;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; ++i)
scanf("%d", &a[i]);
printf("Enter element to find:\n");
scanf("%d", &search);
first = 0;
last = n - 1;
mid = (first + last) / 2;
while (first <= last)
{
if (a[mid] < search)
first = mid + 1;
else if (a[mid] == search)
{
printf("%d found at location %d.\n",search,mid+1);
break;
}
else
last = mid - 1;
mid = (first + last)/2;
}
if (first > last)
printf("Element is not found in array.\n");
return 0;
}
*/