Binary search is an algorithm which can be used to search for an item in a sorted data set.
It discards half of the remaining data with each comparison and repeats the process until the item is found.
On average, a binary search can locate an item in a list in less time than a linear search.
If the data you have is unordered, you must either use a linear search algorithm or sort the data first.
Keywords
binary search
an algorithm to search for an item in a sorted data set — it discards half of the remaining data with each comparison and repeats the process until the item is found or until the data set is exhausted
ordered list
a list of items where the values are in either ascending or descending order
A common mistake
Mistake:A binary search can be performed on any data.
In fact:A binary search can only be performed on an ordered list as it finds the expected position of the search term based on a selected mid-point in the data.
Check what you learned
From Oak National Academy's exit quiz. Choose every answer that is right.
A computing lesson by Oak National Academy licensed under the Open Government Licence (OGL) v3.0.Read the licence
Lesson 2 of 3 · GCSE level, ages 14 to 16
Comparing searching algorithms
By the end:I can compare and contrast linear and binary search and trace the code for both searching algorithms.
The searching algorithm you use will depend on the data and context you are using.
Coding a binary search is more complex than a linear search.
Tracing code for searching algorithms by completing a trace table can help you check how efficient the algorithm is.
Keywords
algorithm
a set of step-by-step instructions to solve a problem
trace table
a tool used to track the values of variables and the flow of execution in an algorithm or program, step by step
A common mistake
Mistake:A linear search algorithm and binary search algorithm are equally complex to write.
In fact:Binary search algorithms are more complex to write due to the different paths that the algorithm can take rather than stepping through the search data in order.
Check what you learned
From Oak National Academy's exit quiz. Choose every answer that is right.
A computing lesson by Oak National Academy licensed under the Open Government Licence (OGL) v3.0.Read the licence
Lesson 3 of 3 · GCSE level, ages 14 to 16
Linear search
By the end:I can explain why computers need to search data and perform a linear search to find the position of an item in a list.