I have a 2D array which is fully sorted. The arrays below are examples
1 2 3
5 6 7
9 10 11
and
1 2 3 4 5
6 7 8 9 10
I would like to use binary search on these array. Let rows
be the number of rows and cols
be the number of columns
Initially start = 0
and end = rows * cols -1
In the 3 X 3 array above, midpoint works out to be four [9 elements]. Now how do I find out the corresponding row and column with the midpoint ? Is there any standard formula for that ?
The formula is pretty simple:
row = number/cols_per_row;
col = number%cols_per_row;
Let size = rows * cols
mid = size // 2
(integer division)
row = mid // cols
col = mid % cols
(rest for integer division)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.