0% found this document useful (0 votes)
17 views1 page

Caut Are

The document describes two different search algorithms: 1) Binary search is used when elements are ordered and increases efficiency by repeatedly dividing the search space in half. 2) Sequential search is used when elements are unordered and checks each element one by one to find a match. 3) The code samples demonstrate implementing binary search to efficiently find a target number in a sorted array and sequential search to linearly search an unsorted array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Caut Are

The document describes two different search algorithms: 1) Binary search is used when elements are ordered and increases efficiency by repeatedly dividing the search space in half. 2) Sequential search is used when elements are unordered and checks each element one by one to find a match. 3) The code samples demonstrate implementing binary search to efficiently find a target number in a sorted array and sequential search to linearly search an unsorted array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Cautare binara

E folosita cand elementelew sunt ordonate


crescator.
int i,n,v[1000],p,m,x,q;
bool gasit=false;

if(gasit==true)
cout<<"Gasit";
else
cout<<"NU EXISTA";
return 0;

cin>>n;
for(i=1;i<=n;i++)
cin>>v[i];
cin>>x; //nr pe care vrem sa-l gasim
q=n;

p=1;

while(p<=q && !gasit)

Cautare secventiala
E folosita cand elementele nu sunt ordonate.
int v[1000],i,n,x;
cin>>n;
for(i=1;i<=n;i++)

cin>>v[i];
m=(p+q)/2;
if(v[m]==x)
{

cin>>x; //nr pe care vrem sa-l cautam


for(i=1;i<=n;i++)
{

gasit=true;

if(v[i]==x)

p=n;

cout<<"Gasit";

else

i=n+1;//se distruge for-ul

}
if(v[m]<x)
p=m+1;
else
q=m-1;

}
}

}
if(i!=n+2) // Daca nr cautat e gasit i-ul
devine n+1, dar mai trece prin acel for si il
face n+1+1. Daca nr nu este gasit dupa ce
executa for-ul i-ul o sa fie n+1 la final.
cout<<"NU EXISTA";
return 0;

You might also like