Linear search

Computer Science · Algorithms

Question 1 of 10

0

    Linear search checks items:

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Linear search explained

    Linear search finds an item in a list by checking each element in turn, starting at the first and moving to the last, until it finds the target or runs out of items. It works on any list, whether or not the items are sorted.

    The steps are:

    1. Start at the first item.
    2. Compare the current item with the target.
    3. If they match, stop and report the position.
    4. Otherwise move to the next item and repeat.
    5. If the end of the list is reached without a match, report that the item is not there.

    In the worst case the item is last or missing, so all nn items are checked, and the time complexity is O(n)O(n). When the item is present, about half the items are checked on average. Linear search is simple, but it becomes slow for very long lists, which is why sorted data is often searched with binary search instead.

    A common mistake is to think linear search needs a sorted list. It does not, which is exactly why it is useful for unsorted data. Another is to forget step 5, so the program runs past the end of the list and crashes when the item is missing.

    Worked example

    Search for 18 in the list [14, 3, 27, 9, 41, 18, 6, 30], which has 8 items. Positions start at 0.

    1. Compare 18 with 14, 3, 27, 9, 41 and then 18. There is no match until the sixth item.
    2. The 18 is at position 5, so 6 comparisons are made.
    3. Searching for 50 instead, which is missing, checks all 8 items: 8 comparisons.

    Key facts

    • The practice questions number the positions from 1: if the item is in position 6, then 6 items are checked. The worked example above counted from 0, so its position 5 meant 6 checks. When a question says "position", the number of checks equals that position. When it says "index" and counts from 0, add 1.
    • If the item is missing, every item is checked, so a list of nn items needs nn checks.
    • The hard questions give a list of a few hundred items, with each check taking a few milliseconds, and ask for a time. Multiply, then convert milliseconds to seconds by dividing by 1000. Some run backwards: divide the total time by the time per check to get the length of the list.
    • If the item is equally likely to be in any position, the average number of checks is n+12\dfrac{n+1}{2}.

    Worked example 2

    A missing target is searched for in a list of 640 items. Each check takes 7 ms. How long does the search take, in seconds?

    1. Every item is checked: 640 checks.
    2. Total time: 640×7=4480640 \times 7 = 4480 ms.
    3. Convert: 4480÷1000=4.484480 \div 1000 = 4.48 s.

    Now go backwards. A search with no match took 3150 ms and each check took 9 ms. How many items are in the list?

    1. Number of checks: 3150÷9=3503150 \div 9 = 350.
    2. Every item was checked once, so the list has 350 items.

    Worked example 3

    A list holds 450 items and each check takes 6 ms. The target is at position 120. Find the time for this search, the worst-case time and the average time, each in seconds.

    1. This search: 120 checks, so 120×6=720120 \times 6 = 720 ms =0.72= 0.72 s.
    2. Worst case: the target is last or missing, so 450 checks. 450×6=2700450 \times 6 = 2700 ms =2.7= 2.7 s.
    3. Average: 450+12=225.5\dfrac{450 + 1}{2} = 225.5 checks. 225.5×6=1353225.5 \times 6 = 1353 ms =1.353= 1.353 s.

    Notice that the average is close to half the worst case. Doubling the list roughly doubles every one of these times, which is what O(n)O(n) means.

    Worked example 4

    Search for 8 in [22, 8, 35, 8, 19, 41, 8, 27], with positions numbered from 1. How many items are checked if the search stops at the first match? How many if the program must find every match?

    1. Stop at the first match: 22 is not 8, then 8 matches in position 2. That is 2 checks.
    2. Find every match: all 8 items must be checked, because a match could be anywhere. The matches are in positions 2, 4 and 7.

    The same list shows why linear search needs no sorting: it works on this unsorted list. Binary search would not.

    Common mistakes

    • Using the index (counting from 0) as the number of checks. Add 1.
    • Forgetting to convert milliseconds to seconds. 4480 ms is 4.48 s, not 4480 s.
    • Dividing by 1000 before multiplying, then losing digits. Multiply first.
    • Thinking a missing item takes 0 checks, or fewer than nn. It takes all nn.
    • Saying linear search needs sorted data. That is binary search.

    Exam tips

    • Show the working as "items × time per check = total ms", then the conversion on its own line.
    • For "how many items" backwards questions, the target is missing, so checks equal items.
    • In the average case, state the assumption: the target is equally likely to be anywhere.
    • Name the worst case O(n)O(n) and say why: every item may be checked.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Linear search checks items:

      • One by one in order
      • In random order
      • From the middle
      • All at once
    • Medium

      A list of 4 items is searched with linear search, from the first item. The item you want is in position 3. How many items are checked before it is found, including that item?

    • Hard

      A linear search checks 620 items, one at a time, and the target is not in the list. Each check takes 7 ms. How long does the search take in seconds?