forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchh.cpp
66 lines (62 loc) · 1.26 KB
/
BinarySearchh.cpp
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
int main()
{
int a[100],num,strt,end,mid,len,pos,find=0,t=0;
printf("Enter length of array:");
scanf("%d",&len);
for(int i=0;i<len;i++)
{
printf("Array[%d]:",i);
scanf("%d",&a[i]);
}
for(int i=0;i<len-1;i++)
{
int s=0;
for(int j=0;j<=len-1-i;j++)
{
if(a[j]>a[j+1])
{
a[j]=a[j]+a[j+1];
a[j+1]=a[j]-a[j+1];
a[j]=a[j]-a[j+1];
s=1;
}
}
t++;
if(s==0)
{
printf("\nthe loop has executed %d times",t);
break;
}
}
for(int i=0;i<len;i++)
{
printf("\n %d index had %d value",i,a[i]);
}
printf("Enter number to search:");
scanf("%d",&num);
strt=0;
end=len-1;
while(strt<=end)
{
mid=(strt+end)/2;
if(num>a[mid])
{
strt=mid+1;
}
else if(num<a[mid])
{
end=mid-1;
}
else if(num==a[mid])
{
pos=mid;
find++;
break;
}
}
if(find==0)
printf("Item not found");
else
printf("Item %d found at position %d",num,pos+1);
return 0;
}