-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.c
More file actions
49 lines (45 loc) · 1017 Bytes
/
binary_search.c
File metadata and controls
49 lines (45 loc) · 1017 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
//Binary Search
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, beg, end, mid, list[50], num;
clrscr();
printf("\nHow many elements you have ");
scanf("%d", &n);
printf("\nEnter your list of numbers : ");
for (i = 0; i < n; i++)
{
scanf("%d", &list[i]);
}
for (i = 0; i <= 5; i++)
{
printf("\n\nEnter the number you want to search : ");
scanf("%d", &num);
beg = 0;
end = n - 1;
while (beg <= end)
{
mid = (beg + end) / 2;
if (list[mid] == num)
{
printf("\nFound at position %d", mid + 1);
break;
}
else if (num > list[mid])
{
beg = mid + 1;
}
else if (num < list[mid])
{
end = mid - 1;
}
}
if (beg > end)
{
printf("\nNumber Not Found");
}
}
getch();
}
//Binary Search