Binary Search in Java

This is a Java implementation of the Binary Search in Python I did earlier. Refreshing my Java šŸ™‚

And the example client code:

This outputs:

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s