 Consider this array with a list of two-letter country codes. They're already in alphabetical sorted order. Let's say we want to find ES, the country code for Spain. We can use the fact that the array is sorted to our advantage. We'll set an index to the low and high elements of the array, and mid to the middle element using integer division by 2. ES comes alphabetically before HU, and because the array is in order, that means we don't have to look at HU or anything that comes after it. Instead, we'll set the high marker to one less than the middle index and recalculate the middle point. ES comes after DE, so we set the low marker to one past the middle, eliminating Austria, Australia, and Germany, and recalculate the middle point. We found Spain, and we return its index, 3. What if we'd been looking for Fiji, FJ? In that case, since FJ is bigger than ES, we'd recalculate and now our low and high indices would meet. FJ is less than the middle element, which means we set the high element to the middle minus one, at which point the high and low have switched places. Whenever that happens, that means the item isn't in the array. It so happens that at this point the low index shows where the item would go if it were to be inserted in the array. Now let's write the program that implements this algorithm. Here's our main method. It defines the array of countries, and then, in a do-while loop, it prompts for a country code, reads it in, converts it to uppercase, and does a binary search through the country's array to find the given value. Inside binary search, we'll have an integer, low, set to zero, and high, set to data.length-1, the last element. As long as the high index is greater than or equal to the low index, we'll set the middle element to high plus low, divided by two. If the data at the middle index equals the value we're looking for, we'll return the middle index. Otherwise, if the value compared to the middle index is less than zero, which means the value is less than the middle point, we'll set the high point to mid minus one. Otherwise, since it's not equal or less than, it must be greater than, and we'll set the low point to mid plus one. That ends our loop. The question now comes, what should we return when we get out of the loop if something's not found? We could return minus one. But since low tells us where the not found value should go, why not return negative low? That looks good until you realize what happens when something belongs at position zero. It would return negative zero, and negative zero is indistinguishable from zero. The answer is, let's take negative low and subtract one. That way, something that belonged at position zero, which wasn't in the array, would give us a negative one. Let's compile this, and let's run it. We'll search for ES, found at index three. And again, let's test with the beginning element, and the ending element, and one that isn't in the array at all. And there's our working binary search method. How does this search perform compared to linear search? Really well. Let's say we have a thousand items all in order. Our first test eliminates 500 items from consideration. The next will eliminate 250, then 125, and so on. It will take a maximum of 10 comparisons to find any item in a list of length 1,000. The performance of binary search is proportional to log to the base two of n, but it only works when your data is in sorted order already.