Explore BrainMass

binary search

What is a binary search, and how does it work?

Solution Preview

Binary Search is arguably one of the most useful algorithms you will come across. It accepts a sorted list of numbers, a single integer value x as input, and returns the xth greatest number in the list.

For example, consider the following sorted list of ten integers:
1, 2, 3, 4, 6, 8, 9, 10, 12, 15

Say that we are looking to see if 12 is in the list. First, look at the middle value in the list. We can find this by looking at the indices of the first and last elements. Since we have a list of ten numbers, we look for the element in position (1+10)/2 = 5.5. In our list, this is the number 6. We ...

Solution Summary

This job defines a binary search.