This is a Java implementation of the Binary Search in Python I did earlier. Refreshing my Java 🙂
And the example client code:
index of tiger : 5 index of dog : 1 index of wombat: 6 index of turtle: not found
There are several other options available in the JDK Arrays and Collections classes:
int index = Arrays.binarySearch(sortedArray, key); int index = Collections.binarySearch(sortedList, key);
For a more detailed explanation of binary search, please see my earlier post.