10.09.2013 Views

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

LECTURE NOTES OF ADVANCED DATA STRUCTURE (MT-CSE 110)<br />

EXAMPLE OF BINARY SEARCH<br />

Consider an array a consisting of items‐10, 20, 30, 40,50,60,70<br />

10 20 30 40 50 60 70<br />

Let in this sorted array we have to search item ’70’<br />

Mid= (beg +end)/2= (1+7)/2= 4<br />

Now,70>A [4] So, beg= 4+1 =5<br />

And<br />

End=7<br />

Mid= (5+7)/2 = 6<br />

Again<br />

70>A [6]<br />

Therefore, Beg=7,End=7<br />

Now,<br />

A[item] = A[mid]<br />

i.e. 70=70<br />

Hence we get the output, 70 is searched at location =A [7]<br />

EXAMPLE:<br />

To implement binary search we are provided with a sorted array A which consist<br />

of following elements.<br />

Sorted array A=3, 7, 11, 17, 19, 21, 27<br />

Element to be searched is 7<br />

X=7<br />

Middle=1+7/2=4<br />

Prepared By :­<br />

Er. Harvinder Singh<br />

Assist Prof., CSE, H.C.T.M (Kaithal) Page ‐ 157 ‐

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!