Knowledge Base > Algorithm > Searching
Overview
A binary searchis a searching algorithm for finding a particular value in a sorted list. This algorithm is an example of divide and conquer algorithm (of method 3.7.3.1). In most cases, this algorithm is considered to run better performance than linear search, since it runs at logarithmic time, O(lg n).
Algorithm
Pseudocode
|
This algorithm is performed by comparing the median element in a list to the one you are searching for, and deciding if the median element is greater than, less than, or equal to the target. The result that the median turns out to be too high becomes the top half of the list, and one too low the becomes bottom half of the list. In the next round, the next median is halfway of the new list. Repeat finding the median of the new sub-list, until the match is found and returned or return -1 if not found. Either case, the size of the last sub-list is 1, in the worst case.
Complexity Analysis
The recurrence function is defined as follow:
Let us first see how to illustrate this recursive relation in tree model:
The total is c ⋅ lg n + c
T(n) = T(1) + c ⋅ lg n = Θ(lg n)
Next, we apply divide and conquerer method (of method 3.7.3.1) to solve the
recurrence,
![T(n) = 2 ⋅T(n) +c
2 n c
= 2 ⋅[2 ⋅T(-)+ -]+ c
n 4 2
= 4 ⋅T(4) +2 ⋅c
n- c
= 4 ⋅(2 ⋅T(8) + 4)+ 2⋅c
= 8 ⋅T(n) +3 ⋅c
8
= ... n
= 2k ⋅T(-k)+ k ⋅c
lgn 2 n k
= 2 ⋅T(2lgn)+ lgn⋅c (2 = n ⇒ k = log2n)
= ...
= T (1) + c⋅lg(n)
= Θ (lgn)](kb124x.png)
Source Code
Binary Search is usually implemented using recursion or iteration, and here are both implementations, written in C and Scheme:
Related Topics