Understanding 2D binary search algorithm


The strategy from the earlier example can be extended to create a 2D binary search algorithm. To explain this algorithm, we will use a 2D matrix sorted in ascending order both row-wise and column-wise, and search for a target value. For the algorithm to work, the input array should meet the two conditions below.

  • The 2D matrix should be sorted in rows and columns, meaning each row and each column is sorted in ascending order.
  • The last number in each row should be less than or equal to the first number in the next row.
Loading Image

Valid input for 2D binary search

Liking the course? Check our discounted plans to continue learning.