Because this is called for many times, we should use binary search or at least a while loop instead of for.