Searching algorithms

<< Previous: Pseudocode Next: Sorting algorithms >>

About searching algorithms

Searching algorithms are designed to check whether an item is present in a list of data. They return a value that indicates if the value is found or not found.

There are two searching algorithms you have to be familiar with for your course: the linear search algorithm and the binary search algorithm.

The linear search algorithm

Linear searches are the most basic form of search. They involve checking each term in the list one after the next.

A flag can make this a more efficient process, but only if it is used on a sorted list. By a flag, I simply mean a variable that stores a boolean value. By setting a variable found to false initially and then to true allows you to perform a check and stop the execution of the algorithm if the item is found.

Binary search algorithm

The binary search algorithm is a divide and conquer algorithm. It requires the list to be in order to work.

To perform a binary search the search term is first compared to the middle item in the list. If it is the same then it returns found. If it does not match and the search term is lower than the middle item, then the middle itme and everything larger can be removed. If the search term is higher then the middle term, then the middle term and everything lower is removed.

We then repeat these steps with our new list. This keeps going until the item is found or the list is empty.



Comparing the linear and binary search algorithms

Linear searches have the advantage of working with unordered lists but are very inefficient for all but very short lists. Binary searches are very efficient but must have an ordered list to work.

Knowledge check


Questions:
Correct:

Question text


<< Previous: Pseudocode Next: Sorting algorithms >>

© All materials created by and copyright S.Goff