I don't understand how the following program that finds all negative numbers in a 2-d array is using binary search? I thought binary search worked by taking a sorted list/array, going to middle, and checking if middle value was >, <, or == to the searched for value, and repeating that in the half containing the searched for value. This program checks iteratively for each row in the program (starting at top right of array) if that value is less than 0, and moves down next row if it is.
Also, why does this program what complexity O(row+col)? I thought binary search algorithms have complexity O(log(n)).
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int row = grid.size()-1;
int col = grid[0].size()-1;
int i = 0; int j = col; int count = 0;
while ((i <= row) && (j>=0)){
if (grid[i][j] < 0){
count++;
j--;
if (j < 0){
i++;
j = col;
}
}
else{
i++;
j = col;
}
}
return count;
}
};