A-level Computing 2009/AQA/Problem Solving, Programming, Operating Systems, Databases and Networking/Programming Concepts/Binary search





Binary search locates the position of an item in a sorted array.

A quick way to remember how this search works is to imagine searching for words beginning with 'S' in a dictionary. You know that the dictionary is in alphabetical order but you don't yet know if there are any words beginning with 'S' in it, nor do you know where the 'S' section begins and ends.

how efficient is it? how many searches for x number of items? what is the equation? give an example. Why is it better than a linear search. Can you use it on ordered lists or unordered lists?

How to work out the maximum number of searches (n) on a x sized array: 2^n > size of list (x) where n = max searches For example, if we had a 3 item array 2^2 = 4 > 3 therefore max searches = 2

Note: This code assumes 1-based array indexing. For languages that use 0-based indexing (most modern languages), min and max should be initialized to 0 and N-1.

Note 2: The code above does not return a result, nor indicates whether the element was found or not.

Note 3: The code above will not work correctly for empty arrays, because it attempts to access an element before checking to see if.

Given a 256 item sorted list, what is the maximum number of searches needed to find an item? 9 as:

2^n > size of list where n = max searches 2^9 = 512 > 256

Given a 1000 item sorted list, what is the maximum number of searches needed to find an item? 10 as: 2^n > size of list where n = max searches 2^10 = 1024 > 1000