Escolar Documentos
Profissional Documentos
Cultura Documentos
Comparing n with thousands of elements will give to much running time in the worst
case when n doesnt exist in the list. The best case of this algorithm is finding n at
the beginning of the imput list.
In order to implement this algorithm we will use a simple while loop and as long as
we have more elements in the list , we will divide and compare with the middle.
Bainari sarci hez meni rial laif aplicheisans , for egzampal , ui chen tinc about a ful
hardisc. For da processor tu acses deita in hardisc , da best metod iz bainari sarci.
Anadar rial laif egzampal ov bainari sarci iz deita beizis. Esium det in e iuniversiti
deita beiz , ui hev tauzands ov bucs. In ordar tu sarci bucs , da best solusian iz
bainari sarci.
In compiutar sarci ui ius bainari sarci in meni algoritrimic cuestans.Tu implement diz
farst algoritm ui uil iuz folauing logik
Code :
void BinarySearch(int a[] , int left , int right , int size)
{
left = 0; right = size-1;
while( left <= right )
{
int m= (left+right) /2;
if(a[m] < target)
left=m+1;
if(a[m] > target)
right=m-1;
if(a[m] == target)
{
cout << Element found at pos <<m<<cout;
break;
}
}
If(left >= right)
cout<<element not found<<endl;