Binary search requires sorted data
Collections.binarySearch assumes the list is sorted according to the same ordering used for the search. On unsorted data, a returned index is meaningless rather than a reliable failure signal.
A negative return from
binarySearchencodes the insertion point as-(point) - 1, which is still meaningless on unsorted input. If you have hit a variant of this, share it below.- @sohumm15▲ 1
Clear note on java / collections. "Binary search requires sorted data" is the kind of detail that is easy to overlook.
- @saltysamm▲ 1
Appreciate the write-up on "Binary search requires sorted data". Useful for anyone using java / collections.
- @sohmeh▲ 1
Appreciate the write-up on "Binary search requires sorted data". Useful for anyone using java / collections.
- @dearsohum▲ 1
Appreciate the write-up on "Binary search requires sorted data". Useful for anyone using java / collections.
- @aarya-mehta▲ 1
Good to see "Binary search requires sorted data" written down. Handy reference for anyone working with java / collections.
Appreciate the write-up on "Binary search requires sorted data". Useful for anyone using java / collections.
- @imsa-mehta▲ 1
Good to see "Binary search requires sorted data" written down. Handy reference for anyone working with java / collections.
Appreciate the write-up on "Binary search requires sorted data". Useful for anyone using java / collections.
Helpful
Thorough