11_sort_srch_alg

11_sort_srch_alg - Binary Search Can be performed on Sorted arrays Full and balanced BSTs Introduction to Computers and Programming Compares and

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 11 April 12 2004 2 Binary Search • Can be performed on – Sorted arrays – Full and balanced BSTs • Compares and cuts half the work – We cut work in ½ each time – How many times can we cut in half? Binary search is O(Log N) Binary search is O(Log N) 3 Binary Search 11 found 3 comparisons 3 = Log (8) 55 not found 4 comparisons 4 = Log (8) +1 67 52 47 43 42 19 17 11 42 17 11 17 67 52 47 43 42 19 17 67 53 19 17 42 47 47 43 53 4 The Binary Search Algorithm Input: Array to search, element to search for Output: Index if element found, -1 otherwise set Return_Index to -1; -- c1 loop -- c2 (log n +1) set Current_Index to (UB + LB) /2 -- c3 (log n +1) if the LB > UB -- c4 (log n +1) exit ; -- c5 if Input_Array(Current_Index) = element –- c6 log n Return_Index := Current_Index -- c7 exit ; -- c8 if Input_Array(Current_Index) < element -- c9 log n LB := Current_Index +1 -- c10 log n else -- c11 log n UB := Current_Index – 1 -- c12 log n return Return_Index -- c13 T(n) = (c1+c2+c3+c4+c5+c7+c8+c13) + (c2+c3+c4+c6+c9+c10+c11+c12) log n = c’ + c’’log(n) = O(log (n)) Statement Work T(n)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 Merge Sort 5 96 67 12 17 42 11 47 5 96 67 12 17 42 11 47 5 96 67 12 17 42 11 47 5 96 67 12 17 42 11 47 47 11 17 42 47 17 42 11 67
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2012 for the course AERO 16.01 taught by Professor Markdrela during the Fall '05 term at MIT.

Page1 / 8

11_sort_srch_alg - Binary Search Can be performed on Sorted arrays Full and balanced BSTs Introduction to Computers and Programming Compares and

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online