Searching algorithms

<< Previous: Identifying errorsNext: Sorting algorithms >>

What is a searching algorithm?

A searching algorithm is an algorithm for checking whether a value exists in a list. There are 2 searching algorithms you need to know about:

Linear search

A linear search is going through a list one after another until the value is found or the end of the list is reached. Including a flag can make this process more efficient as it allows you to stop searching if you find the value you are looking for. Although the code on the right is longer it would execute more quickly if the search term is near the start of the list and the list is long. In the code below there is an advantage in execution time even with a short list.

Binary search

The binary search algorithm is a divide and conquer algorithm. This means it halves the search list every time. A key shortcoming of the binary search method is that the list has to be sorted in order before it can be used.

Comparing linear and binary search

Knowledge check


Questions:
Correct:

Question text


<< Previous: Identifying errorsNext: Sorting algorithms >>

© All materials created by and copyright S.Goff