Binary Search

  1. Big O of Binary Search:

    To calculate the worst-case Big O for Binary Search for an array of size n consider the following:
    After 1 iteration,s there are n/21 (n/2) elements left to search.
    After 2 iterations, there are n/22 (n/2/2) elements left to search.
    After 3 iterations, there are n/23 (n/2/2/2) elements left to search.
    After 4 iterations, there are n/24 (n/2/2/2/2) elements left to search.
    After k iterations, there are n/2k elements left to search.
    In the worst case, searching stops when there is 1 element left to search, so n/2k = 1
    Rearranging the equation: 2k = n
    Taking the log of both sides: log2 2k = log2 n
    Simplifying: k log2 2 = log2 n
    Simplifying: k = log2 n

    Requires log2 n steps/iterations, therefore the O( log2 n ) = O( log n )

Last Modified: