Binary search
A binary searchA method of searching in which the data being searched is halved with every step. is an efficient method of searching an ordered list.
A binary search works like this:
- Start by setting the counter to the middle position in the list.
- If the value held there is a match, the search ends.
- If the value at the midpoint is less than the value to be found, the list is divided in half. The lower half of the list is ignored and the search keeps to the upper half of the list.
- Otherwise, if the value at the midpoint is greater than the value to be found, the upper half of the list is ignored and the search keeps to the lower half of the list.
- The search moves to the midpoint of the remaining items. Steps 2 through 4 are repeated until a match is made or there are no more items to be searched.
Consider this list of ordered numbers:
Suppose we were to search for the value 11.
The midpoint is found by adding the lowest position to the highest position and dividing by 2.
Highest position (8) + lowest position (0) = 8
8/2 = 4
If the answer is a decimal, round up. For example, 3.5 becomes 4. We can round down as an alternative, as long as we are consistent.
Check at position 4, which has the value 7.
7 is less than 11, so the bottom half of the list (including the midpoint) is discarded.
The new lowest position is 5.
Highest position (8) + lowest position (5) = 13
13/2 = 6.5, which rounds up to 7
Check at position 7, which has the value 14.
14 is greater than 11, so the top half of the remaining list (including the midpoint) is discarded.
The new highest position is 6.
Highest position (6) + lowest position (5) = 11
11/2 = 5.5, which rounds up to 6
Check at position 6.
The value held at position 6 is 11, a match. The search ends.
A binary search in pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding. might look like this:
find as integer
found as Boolean
lowerBound as integer
upperBound as integer
midPoint as integer
list[8]
set find = 11
set found = FALSE
set lowerBound = 0
set upperBound = len(list)
while found = FALSE
set midPoint = (upperBound + lowerBound)DIV 2
聽聽聽聽if list[midPoint] = find
聽聽聽聽聽聽聽聽output 鈥淔ound at 鈥&midPoint
聽聽聽聽聽聽聽聽set found = TRUE
else
聽聽聽聽聽聽聽聽if list[midPoint] > find
聽聽聽聽聽聽聽聽聽聽聽聽set upperBound = midPoint-1
else
聽聽聽聽聽聽聽聽set lowerBound = midPoint+1
聽聽聽聽end if
end if
end while
if found = FALSE
聽聽聽聽output 鈥淣ot found鈥
end if
A binary search is a much more efficient algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. than a linear search A function designed to search for the presence of an item stored within an array or database.. In an ordered list of every number from 0 to 100, a linear search would take 99 steps to find the value 99. A binary search would require only seven steps.
However, a binary search can only work if a list is ordered.
More guides on this topic
- The CPU - Eduqas
- Primary storage - Eduqas
- Secondary storage and embedded systems - Eduqas
- Networks - Eduqas
- Internet and cybersecurity - Eduqas
- Data representation - Eduqas
- Storage and data organisation - Eduqas
- Operating systems - Eduqas
- Principles of programming - Eduqas
- Algorithms - Eduqas
- Software development - Eduqas
- Impacts of digital technology on wider society - Eduqas