Binary Search in 2D Array

后端 未结 8 1109
南笙
南笙 2021-02-09 17:14

I wonder, can binary search be applied on a 2D array?

  • What would the conditions on the array be? Sorted on 2D??
8条回答
  •  情歌与酒
    2021-02-09 17:39

    Binary search requires that your array be sorted. Sorting, in turn, requires a total ordering relationship on the array elements. In 1-D it's fairly easy to understand what this means. I think you will have to define a 1-D index into your 2-D array and ensure that the array elements are sorted along that index.

    You have a variety of 1-D indexing schemes to choose from, essentially any space-filling curve will do. The obvious ones which come to mind are:

    • Start with the first element, read along each row, at the end of each row go to the first element in the next row.
    • Same, replace row by column.
    • A diagonalisation, in which you read each diagonal in turn.

    Like @Bart Kiers, I don't understand your 2nd point.

提交回复
热议问题