levo

Algorithms

Linear search

  • 3 things to know
  • Component 02

Overview

Recall questions on the linear search algorithm, tracing successful and unsuccessful searches, best, average and worst-case and space complexity, unsorted data and linked lists, when linear search is the right choice, and checking for the end.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Describe the linear search algorithm.

Key term

Trace a linear search for 7 in the list 4, 9, 2, 7, 5 and count the comparisons.

Key term

Trace a linear search for 6 in the list 4, 9, 2, 7, 5.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Describe the linear search algorithm.

    Model answer

    Start at the first item of the list and compare it with the target value. If it matches, report the position and stop. If not, move to the next item and repeat. If the end of the list is reached without a match, report that the item is not present.

  2. 2

    Trace a linear search for 7 in the list 4, 9, 2, 7, 5 and count the comparisons.

    Model answer

    Compare 4 with 7: no match. Compare 9: no. Compare 2: no. Compare 7: match, so the search reports position four and stops without looking at 5. Four comparisons were needed.

  3. 3

    Trace a linear search for 6 in the list 4, 9, 2, 7, 5.

    Model answer

    Every item is compared in turn: 4, 9, 2, 7 and 5, none of which matches. The end of the list is reached, so the search reports that 6 is not present. Five comparisons were needed, which is the maximum for a list of five items.

  4. 4

    State the best, average and worst-case complexity of a linear search.

    Model answer

    Best case O(1), when the target is the very first item. Worst case O(n), when the target is the last item or is not present at all, since every item must be checked. Average case O(n), because on a successful search of randomly placed data about half the list is examined, and half of n still grows in proportion to n.

  5. 5

    What is the space complexity of a linear search?

    Model answer

    O(1). It needs only a counter for the current position and a copy of the target value, no matter how large the list is, and it does not modify or duplicate the data.

  6. 6

    Does a linear search require the list to be in any particular order?

    Model answer

    No. It makes no assumption about order and simply examines every item until it finds the target or runs out. This is its main advantage over binary search, which cannot be used at all unless the data is sorted.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Algorithms, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.