Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, t[100], c=0;
- float v;
- void caut(int s, int d)
- {
- if(s==d)
- {
- if(v==t[s])
- {
- cout<<"Gasit pe pozitia: "<<s;
- c++;
- }
- }
- else if(s<d)
- {
- int m=(s+d)/2;
- if(v==t[m])
- {
- cout<<"Gasit pe pozitia: "<<m;
- c++;
- }
- else if(v>t[m])
- {
- caut(s, m-1);
- c++;
- }
- else
- {
- caut(m+1, d);
- c++;
- }
- }
- else
- cout<<"nu exista";
- }
- int main()
- {
- cin>>n>>v;
- for(int i=1; i<=n; i++)
- cin>>t[i];
- caut(1, n);
- cout<<endl<<"Nr comparari: "<<c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement