0% found this document useful (0 votes)
36 views1 page

Search

Binary search examines the middle element of a sorted list to determine if it is the sought element, or if the element is in the first or second half of the list. It repeatedly halves the list to narrow down the search location. To calculate the maximum number of comparisons for a binary search of a list, take the logarithm base 2 of the number of items, as binary search may require examining each half down to a single item remaining.

Uploaded by

ImaginRtist
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

Search

Binary search examines the middle element of a sorted list to determine if it is the sought element, or if the element is in the first or second half of the list. It repeatedly halves the list to narrow down the search location. To calculate the maximum number of comparisons for a binary search of a list, take the logarithm base 2 of the number of items, as binary search may require examining each half down to a single item remaining.

Uploaded by

ImaginRtist
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Search

Sunday, January 27, 2013 7:41 PM

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

Computer Science Page 1

You might also like