Binary search runs in O(log n) time.

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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.three possibilities: we could take the first branch of the if statement (i....
View Full Document

This note was uploaded on 09/20/2007 for the course COM S 211 taught by Professor Kozen during the Spring '06 term at Cornell.

Page1 / 3

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

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

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