Algoritm pentru cautarea binara

"Se da un sir de numere ordonat crescator cu N elemente, se cere sa se returneze pozitia in sir a unui numar introdus de la tastatura."

Cum lucreaza algoritmul?

Vom avea 3 variabile:
Stanga : inceputul intervalului in care cautam
Mijloc : mijlocul intervalului
Dreapta : sfarsitul intervalului in care cautam

Le voi prescurta St,Dr,Mij in program.Practic cele 3 sunt prima pozitie, pozitia mijlocie si ultima pozitie din vector.

Algoritmul verifica de repetate ori daca mijlocul este egal cu numarul cautat, returnand pozitia sa, insa intervalul se modifica odata cu parcurgerea.Astfel vom impune 3 conditii, cat timp St<=Dr:
C++ Code:
You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
-in cazul acesta, daca conditia este adevarata elementul a fost gasit
C++ Code:
You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
-daca nr este mai mic decat v[Mij], Dr va deveni Mij
C++ Code:
You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
-daca nr este mai mare decat v[Mij], St va deveni Mij


C++ Code:
You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!


Exemplu:
Code:
You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Algoritmul va lucra in felul urmator: va citi numerele, apoi cand ajunge la apelarea functiei intram in ea.
Code:
You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!