Re #9682. Implemented binary search

Replaced the linear search with a binary search and added unit tests.