Level 3

Binary Search

Binary search is the most efficient searching algorithm having a run-time complexity of O(log2 N). This algorithm works only on a sorted list of elements.

Binary search begins by comparing the middle element of the list with the target element. If the target value matches the middle element, its position in the list is returned. If it does not match, the list is divided into two halves. The first half consists of the first element to middle element whereas the second half consists of the element next to the middle element to the last element. 

Let's take an example and see how binary search works and number of steps it takes as compared to linear search.

 

Binary Search example

Important Tutorials

1. Binary Search Implementations And Common Errors
View Tutorial
2. Beyond Sorted Array Binary Search
View Tutorial

Binary Search Problems

Search answer
Problem Score Companies Time Status
Matrix Median 225
63:06
Square Root of Integer 275 40:00
Painter's Partition Problem 350
67:23
Allocate Books 350
57:40
Simple binary search
Problem Score Companies Time Status
Matrix Search 250
35:06
Search for a Range 250
39:48
Sorted Insert Position 250
23:36
Search step simulation
Problem Score Companies Time Status
Implement Power Function 275
57:25
Sort modification
Problem Score Companies Time Status
Rotated Sorted Array Search 325
53:47
Median of Array 325 71:23