repl.it
@ABHISHEKAREER/

BINEARY SEARCH IN DATA STRUCTURE

C++

No description

fork
loading
Files
  • main.cpp
main.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
66
67
68
#include<iostream>
using namespace std;
 int main()
 {
   cout<<"Name :- Abhishek kareer"<<"\n"<<"Roll no :- 1611981018"<<endl;
   int a[100],b,c,i,j,temp,q;
   cout<<"enter the size";
   cin>>c;
   for(i=0;i<c;i++)
   {
     cin>>a[i];
   }
   for(i=0;i<c-1;i++)
   {
     for(j=0;j<c-i-1;j++)
     {
       if(a[j]>a[j+1])
       {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
       }
     }
   }
   int  begin=0;
   int  end=c-1;
   int middle=(begin+end)/2;
   cout<<"enter the no to search";
   cin>>q;
   if(q!=0)
{
  int flag=0;
if(a[middle]==q)
{
cout<<"found in middle";
flag=1;
}
else if(q>a[middle])
{
for(i=middle;i<=end;i++)
{
 if(q==a[i])
{
cout<<"found in part 2";
flag=1;
break;
}
}
}
else 
{
for(i=middle;i>=begin;i--)
{
 if(q==a[i])
{
cout<<"found in part 1";
flag=1;
break;
}
}
}
if(flag==0)
{
  cout<<"not found!";
}
}
return 0;
}
Fetching token
?