Binary search runs in O(log n) time.

Binary search runs in O(log n) time. - Binary search runs...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Binary search runs in O (log n ) time. Michael George Tuesday March 29, 2005 This is a proof that binary search runs in O (log n ) time. Here is the code: binsearch ( A, x, a, b ) if b = a then return false m b - a 2 + a if A [ m ] > x then return binsearch ( A, x, a, m ) else if A [ m ] = x then return true else if A [ m ] < x then return binsearch ( A, x, m, b ) Let C be the amount of required to run all of the code in the procedure except for the two recursive calls, and let T ( n ) be the total amount of time required to run the procedure when b - a = n . I claim that T ( n ) C log n + T (1) for all n 1. I will prove this by strong induction. The base case (when n = 1) is clear: C log 1 + T (1) = 0 + T (1) = T (1) Now, choose a particular n > 1. For our inductive hypothesis we will assume that for all k < n , that T ( k ) C log k + T (1). How long does binsearch take to run if b - a = n ? Well, there are three possibilities: we could take the first branch of the if statement (i.e. A [ m ] > x ), we could take the second branch ( A [ m ] = x ), or we could take the third branch ( A [ m ] < x ).
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern