What is the best way to handle null during binary search on List<string> (well, it would be List<string> if I could read all the values in advance)?
int previous = 0; int direction = -1; if (itemToCompare == null) { previous = mid; for (int tries = 0; tries < 2; tries++) { mid += direction; itemToCompare = GetItem(mid); while (itemToCompare == null && insideInclusiveRange(min, max, mid)) { mid += direction; itemToCompare = GetItem(mid); } if (!insideInclusiveRange(min, max, mid)) { mid = previous; direction = -direction; } else if (itemToCompare != null) { break; } } }
I am currently doing something like the above - if zero occurs, then linearly search in the direction until none of the non-empty or end points meet, unless success repeats in the other direction. In the actual code, I get direction from the previous comparison result, and GetItem() caches the values it retrieves. Is there an easier way without doing an intermediate list of non-zero values (takes too much time for my purposes, because the GetItem() function above is slow)?
I suppose I'm asking if there is a smarter way to handle null values than degrading to a linear search. In all likelihood, there will be only a small percentage of zeros (1-5%), but it is possible that there will be sequences of 100 with zero.
Change The data looks something like this.
aa aaa
b bb bbb
c cc
d ddd
where each row is a separate object, and not all cells are guaranteed to be filled. The user should be able to search the entire line (so that both "bb" and "bbb" match the entire second line). The query of each object is slow enough so that the linear search does not work. For the same reason, creating a new list without zeros is not feasible.
source share