Linear search and bubble sort Cambridge IGCSE Computer Science revision

Not started

Learn it

In plain words

Two jobs come up constantly with lists: finding an item, and putting the items in order. The syllabus asks for the simplest method of each.

5 things to know

  1. A linear search checks each item in turn, starting from the first, until the item is found or the end of the list is reached.
  2. A linear search works on any list, sorted or not. It can be slow for a long list, because every item may have to be checked.
  3. A bubble sort compares each pair of items that are next to each other, and swaps them if they are in the wrong order.
  4. One trip through the whole list is called a pass. After the first pass, the largest item has moved to the end.
  5. Passes are repeated until one goes right through with no swaps. The list is then in order.

Worked example

Sort the list 5, 2, 8, 1 into ascending order using a bubble sort.

  1. Pass 1: 5 and 2 swap, giving 2, 5, 8, 1. 5 and 8 stay. 8 and 1 swap, giving 2, 5, 1, 8.
  2. Pass 2: 2 and 5 stay. 5 and 1 swap, giving 2, 1, 5, 8. 5 and 8 stay.
  3. Pass 3: 2 and 1 swap, giving 1, 2, 5, 8. The rest stay.
  4. Pass 4: no swaps are made, so the list is sorted: 1, 2, 5, 8.

Tips and tricks

  • A bubble sort only ever compares neighbours. Show the list after each swap, not only at the end.
  • The sort has not finished until a pass makes no swaps. That last, empty pass is how the algorithm knows it is done.
6 questions, about 2 minutes.

It lands in your notebook with its questions as flashcards.

Linear search and bubble sort: 6 questions and answers

These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.

  1. How does a linear search work?
    • It starts in the middle of the list.
    • It checks each item in turn from the start. (the answer)
    • It sorts the list first.
    • It checks only the last item.

    It carries on until the item is found or the list ends.

  2. A linear search looks for a name that is the fourth item in a list of ten. How many items are checked?
    • 1
    • 4 (the answer)
    • 6
    • 10

    The first, second, third and fourth.

  3. In a bubble sort, which items are compared?
    • the first and the last
    • items that are next to each other (the answer)
    • every item with the middle one
    • random pairs

    Neighbours are swapped if they are in the wrong order.

  4. After the first pass of a bubble sort into ascending order, where is the largest item?
    • at the start
    • in the middle
    • at the end (the answer)
    • it has been removed

    It "bubbles" along to the end.

  5. How does a bubble sort know the list is in order?
    • It counts ten passes.
    • A whole pass is made with no swaps. (the answer)
    • The first item is the smallest.
    • The user tells it.

    No swaps means no pair is out of order.

  6. What is the list 6, 3, 8, 5 after the first pass of a bubble sort into ascending order?
    • 3, 5, 6, 8
    • 3, 6, 5, 8 (the answer)
    • 6, 3, 5, 8
    • 3, 6, 8, 5

    6 and 3 swap. 6 and 8 stay. 8 and 5 swap.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf