{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

11_sort_srch_alg

# 11_sort_srch_alg - Binary Search Can be performed on Sorted...

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

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) 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 47 53 19 42 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)

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

View Full Document
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 12 5 96 67 12 5 96 5 11 12 47 17 42 67 96 6 Merge Sort Analysis Analysis MergeSort (A, left, right) T(n) if (left < right) O(1) mid := (left + right) / 2; O(1)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern