Binary Search Code

 

Putting It All Together

The output should be $3$ or $4$ or $5$:

  4
  

 

Time Complexity

  • Best case: $O(1)$ - The key happens to be at the very first midpoint examined.
  • Average case: $O(\log n)$ - On average you chop the search space in half until one element is left.
  • Worst case: $O(\log n)$ - You exhaust all halvings (e.g., key not present or at extreme end).

 

Code on GitHub

See the implementation code for Binary Search algorithm.

Last updated 11 May 2025, 02:05 +0500 . history