Search
Search
Binary search A binary search examines the middle element of the sorted list. It either finds the element, determines that the element being sought is less in value than the middle item (which then implies that the element is in the first half of the list), or determines that the element being sought is greater in value than the middle item (which then implies that the element is in the second half of the list). It keeps splitting the list in half in hopes of finding the value (which is called making a comparison). If you are asked the number of comparisons it will take to perform a binary search on a certain number of items, you always take the worst case scenario (the most comparisons possible.) Ex: A binary search of 1000 items will take at most how many comparisons? Work: Keep dividing until you reach one item. Items in list that could be item Number of comparisons 1000 500 250 125 63 32 16 8 4 2 1 0 1 2 3 4 5 6 7 8 9 10
Even with one element remaining, a comparison is needed in case the remaining element is not the one being sought. Answer: 10 comparisons