Explore BrainMass

Detailed explanation of a binary search in Java

Examine the program. Write a Word or text document detailing how the binary search code works.

Text document ok.

See the attached file.


Solution Preview

The basic idea of a binary search is fairly straight-forward. You start with an ordered list of elements and an element to search for in that list. Then you look at the middle element of the list, if it matches the search element you are done. If the middle element is larger than the search element then you know that you only need to search the first half of the list for the element (since if the search element is in the list and the list is ordered it will be in all of the elements lower than the middle). At this point you repeat the procedure pretending that the first half of the list is the entire list that you have. That is, you look at the middle of the new list (the first half of the old list) and look for a match and so on. You repeat this process until ...

Solution Summary

In this solution I provide a detailed explanation of how a binary search works. This includes a complete example and illustration using a Java implementation of the binary search.