-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search.c
91 lines (76 loc) · 1.95 KB
/
Binary_Search.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<stdio.h>
void bin_ser(int *arr, int item, int i, int first, int last, int middle, int flag)
{
first=0;
last=9;
middle= (first+last)/2;
for (i=0; i<9; i++)
{
if (arr[middle]>item)
{
last=middle-1;
middle= (first+last)/2;
}
else if (arr[middle]==item)
{
printf("The element position is : %d\n", middle+1);
flag++;
break;
}
else if (arr[middle]<item)
{
first=middle+1;
middle= (first+last)/2;
}
}
if (flag==0)
{
printf("The element is not present\n");
}
}
int main()
{
printf("------------------Binary Search using function---------------------\n");
int item, i, first, last, middle, flag=0;
int arr[10]={11,23,35,43,59,62,76,83,91,108};
printf("Enter the element to search :\n");
scanf("%d", &item);
bin_ser(arr, item, i, first, last, middle, flag);
return 0;
}
// -----------------------------------------------------------------
// #include<stdio.h>
// int main()
// {
// int item, i, result, first, last, middle, flag=0;
// int arr[10]={11,23,35,43,59,62,76,83,91,108};
// printf("Enter the element to search :\n");
// scanf("%d", &item);
// first=0;
// last=9;
// middle= (first+last)/2;
// for (i=0; i<9; i++)
// {
// if (arr[middle]>item)
// {
// last=middle-1;
// middle= (first+last)/2;
// }
// else if (arr[middle]==item)
// {
// printf("The element position is : %d\n", middle+1);
// flag++;
// break;
// }
// else if (arr[middle]<item)
// {
// first=middle+1;
// middle= (first+last)/2;
// }
// }
// if (flag==0)
// {
// printf("The element is not present");
// }
// return 0;
// }