Say we have an unsorted pile of tests and we go looking for the one that belongs to one particular student. In the best case, that paper might be at the top of the pile - it would only take one unit of work. In the worst case, if we go through the pile from top to bottom, it would be the bottom paper in the pile - we would have to look at every single paper. This worst case would also apply if the paper was not even in the stack - we would have to check every paper to figure that out. The average would be somewhere between those two: sometimes we get lucky and find it early, and sometimes we are less lucky - in general, we would have to look at half of the papers.
Note that in terms of Big-O
is the same as
- they are both linear functions. While it is true that in the best case, the algorithm takes 1 unit of time regardless of the number of papers, we can’t count on always having the one we want at the top of the pile. Based on our rating of this algorithm on this rare exception would be misleading. So instead we will say Linear Search is
Kind of makes sense that linear search is a linear algorithm!