-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_Binnary Search.c
70 lines (65 loc) · 1.23 KB
/
20_Binnary 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
// Binary Search Implementation program
// The given List must be shorted
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],lft=0,rt=9, mid=(lft+rt)/2,key,i;
printf("Note-[Please enter the elements in the ascending order]\nEnter the elements of the arry:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value to search: "); // Entering the value to search
scanf("%d",&key);
while(lft<=rt)
{
if(key<a[mid])
{
rt=mid-1;
}
else if(key>a[mid])
{
lft=mid+1;
}
else
{
printf("The element is found at location: %d",mid+1);
break; // jumps out of the loop block
}
mid=(lft+rt)/2; // Updating the value of mid
}
if (lft>rt)
{
printf("\nThe element is not found !");
}
return 0;
// int a[10]={
// {10,20,30},
// {40,50,60},
// {70,80,90}};
// int i,lft=0,rt=8,key;
// int mid=lft+rt/2;
// printf("Enter the number you want to search: ");
// scanf("%d",&key);
// for(i=0;i<8;++i)
// {
//
// if(a[mid]<key)
// {
// lft=mid+1;
// }
// if(a[mid]>key)
// {
// rt=mid-1;
// }
// if(a[mid]==key)
// {
// printf("The Elemet is found at mid location %d",mid );
// break;
// }
// mid=lft+rt/2;
// }
// printf("\nNo such value is found !");
// return 0;
}