previous |
start |
next
Binary Search
- Locates a value in a sorted array by determining whether the
value occurs in the first or second half, then repeating the search
in one of the halves
- Number of visits to search an sorted array of size n
- We visit one element (the middle element) then search either
the left or right subarray
Thus: T(n) = T(n/2) + 1
- It n is n/2, then
T(n/2) = T(n/4) +
1
- Substituting into the original
equation: T(n) = T(n/4) +
2
- This generalizes to: T(n) =
T(n/2k) + k
- Assume n is a power of 2, n =
2m
Then: T(n) =
T(n/2m) + m
- Since m = log2(n),
then: T(n) = 1 +
log2(n)
- Binary search is a O( log(n) ) algorithm
previous |
start |
next