Binary Search Algorithm - Performance

Performance

With each test that fails to find a match at the probed position, the search is continued with one or other of the two sub-intervals, each at most half the size. More precisely, if the number of items, N, is odd then both sub-intervals will contain (N - 1)/2 elements, while if N is even then the two sub-intervals contain N/2 - 1 and N/2 elements.

If the original number of items is N then after the first iteration there will be at most N/2 items remaining, then at most N/4 items, at most N/8 items, and so on. In the worst case, when the value is not in the list, the algorithm must continue iterating until the span has been made empty; this will have taken at most ⌊log2(N) + 1⌋ iterations, where the ⌊ ⌋ notation denotes the floor function that rounds its argument down to an integer. This worst case analysis is tight: for any N there exists a query that takes exactly ⌊log2(N) + 1⌋ iterations. When compared to linear search, whose worst-case behaviour is N iterations, we see that binary search is substantially faster as N grows large. For example, to search a list of one million items takes as many as one million iterations with linear search, but never more than twenty iterations with binary search. However, a binary search can only be performed if the list is in sorted order.

Read more about this topic:  Binary Search Algorithm

Famous quotes containing the word performance:

    So long as the source of our identity is external—vested in how others judge our performance at work, or how others judge our children’s performance, or how much money we make—we will find ourselves hopelessly flawed, forever short of the ideal.
    Melinda M. Marshall (20th century)

    The honor my country shall never be stained by an apology from me for the statement of truth and the performance of duty; nor can I give any explanation of my official acts except such as is due to integrity and justice and consistent with the principles on which our institutions have been framed.
    Andrew Jackson (1767–1845)

    Just as the performance of the vilest and most wicked deeds requires spirit and talent, so even the greatest demand a certain insensitivity which under other circumstances we would call stupidity.
    —G.C. (Georg Christoph)