Você está na página 1de 4

Binary search :

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.

A better approach binary search :


Def: Binary search is much faster than simple search sort algorithm. In binary
search , the imput list must be sorted. We can use Quick sort or Heap sort to sort
the imput list. After sorting the imput list. We will apply the following steps in binary
search :
1 Divide the imput list into 2 (by 2).
2 Chech the middle element with n. If n is equal to middle element we display cout
middle element.
3 If the middle element is not equal to n , we will compare .If n is middle element ,
we will apply step 2 for the left of middle element. If n is bigger than middle , we
apply right side of middle element.

4 6 7 8 8 9 10 11 12 13 14 15 19 20 -> sort the list ( Quick or Heap )


Middle
If ( middle == n )
Finish -> display pos of middle
If ( n < middle )
Split left part and check middle
If ( n > middle )
Split right part and check right

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.

Binary search implementation :


Binary search has many real life applications , for example , we can think about a
full hard disk . For the processor to access data in hard disk , the best method is
binary search . Another real life example of binary search is data basese. Assume
that in a university data base , we have thousands of books. In order to search
books. , the best solution is binary search. In computer search we use binary search
in many algorithmic questions. To implement this fast algorithm we will use the
following logic.

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

Bainari sarci tri(kopak):


Aftar larning bainari sarci , ui uil continiu uit bainari sarci deita stracciar. Diz deita
stracciar iz veri efficient for eding , rimuvin , end sarcing elements.
Da general rul ov bainari sarci tri starting from da rut element left ciald shud bi
smolar, rait shud bi bigar.
Uen ui ed elements ui hev tu check starting for rut if da element is smalar or bigar
ricarsivli. Ricarsiv cals ar veri important , dei provaid as vei ov compering left end
rait ciaild elements. For egzampal , uen ui uant tu includ 200 , ui shud farst cek uit
20 end den bi iuzing compering col , tu comper uit 25 end den da last recarsiv cal
compering uit 100.

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;

Finding first and last occurance of a number :

Bainari sarci iz iuzed tu faind if an element results or not .


Ui can adapt diz algoridm intu fainding farst end last ocarans az falauing:
int BinarySearch( int a[], int size )
{
int left = 0 , right = size -1;
int m =(left + right ) /2;
while (left <= right )
{
if (a[m] == target )
return m;
else if ( a[m] > target )
right = m-1;
else
left = m+1;
}
return -1;
}
This algorithm will just find the target or not. We can update this algorithm for
optaining to return first and last occurance by using an extra variable. Once we find
the target number in the list , instead of returning target number. We will update left
or right positions.

Você também pode gostar