HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Standard Algorithms: Totals, Searching, Sorting and Validation
  1. 1Totalling, counting, maximum, minimum and average
  2. 2Linear search and bubble sort
  3. 3Validation and verification
  1. Computer Science
  2. /Standard Algorithms: Totals, Searching, Sorting and Validation
  3. /Linear search and bubble sort

Course 7.3·Lesson 2 of 3

Linear search and bubble sort

A linear search looks through a list item by item until it finds what it wants. A bubble sort puts a list in order by comparing neighbours and swapping them. You write both in pseudocode, dry-run them, and count their comparisons and passes.

about 25 min1 simulation1 practice question
Lesson 2 of 3Show all lessonsHide
Standard Algorithms: Totals, Searching, Sorting and Validation
  1. 1Totalling, counting, maximum, minimum and average
  2. 2Linear search and bubble sort
  3. 3

Try the code and the simulations beside the lesson.

Looking for something in a list

How do you find a friend's name on an unsorted class list? You start at the top and read each name until you find it, or reach the end. That is a linear search.

The lists in this lesson are arrays: a set of values under one name, each at a numbered position called an index. Names[3] is the value at index 333. Arrays are taught fully in the next chapter. Here we only read and swap their values.

Linear search

The array Names holds Ravi, Lena, Omar, Mei, Tariq and Zoe, at the indexes 111 to 666.

pseudocode
DECLARE Names : ARRAY[1:6] OF STRING
INPUT Wanted
Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= 6 DO
    IF Names[Index] = Wanted
      THEN
        Found ← TRUE
      ELSE
        Index ← Index + 1
    ENDIF
ENDWHILE
IF Found = TRUE
  THEN
    OUTPUT Wanted, " found at position ", Index
  ELSE
    OUTPUT Wanted, " not found"
ENDIF
Six boxes in a row hold the names Ravi, Lena, Omar, Mei, Tariq and Zoe at the positions 1 to 6. Ravi, Lena and Omar are shaded red with check 1, check 2 and check 3 above them. Mei is shaded green with check 4. A caption says: Searching for Mei: found at position 4 after 4 checks.

The loop stops for one of two reasons: the name is found, or every index has been checked. The flag Found records which. Searching for Mei takes 444 comparisons. Searching for a name that is not there takes all 666.

A linear search works on any list, sorted or not. Its weakness is speed: a long list may need a comparison for every item. The Python Essentials chapter "Searching and Sorting" also shows a faster search for sorted lists, which this syllabus does not ask for.

Bubble sort

A bubble sort puts values in order by comparing neighbours. If a pair is in the wrong order, it is swapped. One sweep from the start to the end is a pass. After the first pass, the largest value has "bubbled" to the end. Each pass puts one more value in its final place.

pseudocode
DECLARE Scores : ARRAY[1:5] OF INTEGER
Last ← 5
REPEAT
    Swapped ← FALSE
    FOR Index ← 1 TO Last - 1
        IF Scores[Index] > Scores[Index + 1]
          THEN
            Temp ← Scores[Index]
            Scores[Index] ← Scores[Index + 1]
            Scores[Index + 1] ← Temp
            Swapped ← TRUE
        ENDIF
    NEXT Index
    Last ← Last - 1
UNTIL Swapped = FALSE OR Last = 1

Three lines swap a pair, with Temp holding one value for a moment. Without Temp, one value would be lost. The flag Swapped stops the sort early: a pass with no swaps means the list is already in order. Last shortens each pass, because the end of the list is already sorted.

A bubble sort of 7, 3, 9, 1, 5 shown row by row. Start: 7, 3, 9, 1, 5. After pass 1: 3, 7, 1, 5, 9 with the 9 shaded green. After pass 2: 3, 1, 5, 7, 9 with 7 and 9 green. After pass 3: 1, 3, 5, 7, 9 with 5, 7 and 9 green. After pass 4: 1, 3, 5, 7, 9, all green. Green means in its final place.

The picture sorts 777, 333, 999, 111, 555. Follow pass 1.

  1. Compare 777 and 333: swap. 3,7,9,1,53, 7, 9, 1, 53,7,9,1,5.
  2. Compare 777 and 999: no swap.
  3. Compare 999 and 111: swap. 3,7,1,9,53, 7, 1, 9, 53,7,1,9,5.
  4. Compare 999 and 555: swap. 3,7,1,5,93, 7, 1, 5, 93,7,1,5,9.

After 444 comparisons, 999 is in its final place. Pass 2 needs only 333 comparisons, pass 3 needs 222, and pass 4 needs 111. For nnn items a pass makes at most n−1n - 1n−1 comparisons, and the whole sort makes at most n(n−1)2\tfrac{n(n - 1)}{2}2n(n−1)​.

To sort into descending order, change only the comparison: IF Scores[Index] < Scores[Index + 1].

Practise with new numbers

Try it in Python

The grey blocks marked pseudocode are written in the exam style and do not run. The Python box is the same algorithm in Python, and that one runs. Python can swap two values in one line, a, b = b, a, so it needs no Temp. The pseudocode needs the three-line swap.

Read it first. Then run it. Compare the passes with the picture.

1. Search for "Ali". What does the program print, and how many checks does it make?

2. Change the test to scores[i] < scores[i + 1]. In what order are the scores now?

Common mistakes

  • Stopping a linear search too early. Keep going until the item is found or the last index has been checked.
  • A swap without a temporary variable. A ← B then B ← A leaves both equal to B.
  • Going past the end of the array. Compare Index with Index + 1 only up to Last - 1.
  • Thinking one pass sorts the list. One pass fixes only the largest value. Keep passing until a pass makes no swaps.

Remember these

  • Linear search: check each item in turn from the start; stop when it is found or the list ends.
  • Bubble sort: compare neighbours, swap if out of order, repeat passes until there are no swaps.
  • A swap needs a temporary variable in pseudocode.
  • For nnn items, a pass makes at most n−1n - 1n−1 comparisons.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. A linear search looks for Tariq in the list above. How many comparisons does it make?
  1. Do one pass of a bubble sort on 4,2,6,14, 2, 6, 14,2,6,1. What is the list after the pass?
  1. Why does the bubble sort stop when a pass makes no swaps?
  1. How many comparisons does the first pass make on a list of 999 items?
  1. Which one line changes a bubble sort from ascending to descending order?

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (2026, 2027 and 2028), unit 7, part 4: linear search and bubble sort.

Enroll free in Standard Algorithms: Totals, Searching, Sorting and Validation

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (4)

A club keeps its eight members in an array in the order they joined: Ines, Kofi, Hana, Luis, Ama…Easy · High School (9–10) · by StemPy Lab
A bubble sort puts the scores 5, 1, 4, 2, 8 into ascending order…Medium · High School (9–10) · by StemPy Lab
A shop stores the colours of the ten bicycles in its window in an array Colours: red, blue, red…Medium · High School (9–10) · by StemPy Lab
Next: lesson 3Validation and verification
← Standard Algorithms: Totals, Searching, Sorting and Validation
← Totalling, counting, maximum, minimum and averageValidation and verification →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

Validation and verification
A bubble sort is changed so that it sorts in descending order: the comparison becomes IF…Hard · High School (9–10) · by StemPy Lab

Python Labs (1)

Search, sort and checkPython Lab · by StemPy Lab