AS Level · Topic 10.2
A-Level Computer Science 9618 / Topic 10 / AS

Look, then swap.

The two array algorithms the AS syllabus names. A linear search checks elements one at a time until it finds the target or runs out. A bubble sort compares neighbours and swaps them, pass after pass, until the array is in order.

The key idea

A linear search compares the target with each element in turn, from the first, and stops when it is found or the end of the array is reached. It works on unsorted data; in the worst case it makes n comparisons. A bubble sort compares each pair of adjacent elements and swaps them (using a temporary variable) if they are in the wrong order. After each pass the largest unsorted value has reached its final place. The improved version uses a flag to stop as soon as a pass makes no swaps and shortens each pass by one.

bubble sort: the array after each pass startpass 1pass 2pass 3pass 4 251240819 122581940 128192540 812192540 812192540 3 swaps2 swaps1 swap0 swaps: stop green = in its final place: the largest unsorted value bubbles to the end each pass
Fig. 1: an improved bubble sort of five integers, shown after each pass
Section 01

Linear search.

In the lab, search for a value that is in the list and then for one that is not. Count the comparisons each time: the second search has to check every element before it can say "not found".

DECLARE Names : ARRAY[1:8] OF STRING
DECLARE SearchName : STRING
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
INPUT SearchName
Found ← FALSE
Index ← 1
WHILE Index <= 8 AND Found = FALSE
    IF Names[Index] = SearchName THEN
        Found ← TRUE
    ELSE
        Index ← Index + 1
    ENDIF
ENDWHILE
IF Found = TRUE THEN
    OUTPUT SearchName, " found at position ", Index
ELSE
    OUTPUT SearchName, " not found"
ENDIF
Worked example Count the comparisons
Names holds Raj, Ola, Mei, Kit, Zed, Ann, Lou, Ivy in elements 1 to 8. State the output and the number of comparisons with SearchName for (a) Ann (b) Tom.
  1. (a) Compare with Raj, Ola, Mei, Kit, Zed: no match, Index rises to 6.
  2. Names[6] = Ann, so Found becomes TRUE and the WHILE condition is now FALSE: 6 comparisons.
  3. (b) Tom matches none of the 8 names. Index goes up to 9, so Index <= 8 is FALSE and the loop stops: 8 comparisons.
  4. Found is still FALSE, so the second branch of the final IF runs.
Answer: (a) Ann found at position 6 after 6 comparisons; (b) Tom not found after 8 comparisons
Your turn
Why does this linear search use a WHILE loop rather than a FOR loop from 1 to 8?
Examiner trap

Do not put OUTPUT "not found" inside the loop: it would be output for every element that does not match. Decide only after the loop has finished, using the Found flag.

Section 02

Bubble sort.

In the lab, step through a bubble sort. Each step compares two neighbours; at the end of every pass the largest remaining value has bubbled to the end of the unsorted part.

DECLARE Nums : ARRAY[1:5] OF INTEGER
DECLARE Pass : INTEGER
DECLARE j : INTEGER
DECLARE Temp : INTEGER
FOR Pass ← 1 TO 4
    FOR j ← 1 TO 4
        IF Nums[j] > Nums[j + 1] THEN
            Temp ← Nums[j]
            Nums[j] ← Nums[j + 1]
            Nums[j + 1] ← Temp
        ENDIF
    NEXT j
NEXT Pass
Worked example One pass by hand
Nums holds 25, 12, 40, 8, 19. Show the array after the first pass of the bubble sort.
  1. j = 1: 25 > 12, swap: 12, 25, 40, 8, 19.
  2. j = 2: 25 > 40 is FALSE, no swap.
  3. j = 3: 40 > 8, swap: 12, 25, 8, 40, 19.
  4. j = 4: 40 > 19, swap: 12, 25, 8, 19, 40. Three swaps; 40 is now in its final place.
Answer: 12, 25, 8, 19, 40
Your turn
Continue the sort. What does the array hold after pass 2?
Examiner trap

Writing the swap as Nums[j] ← Nums[j + 1] then Nums[j + 1] ← Nums[j] copies one value into both elements. Always use a temporary variable.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

A linear search looks for a value that is not in an array of 8 elements. How many comparisons does it make?

Quick check+10 XP

In the lab's bubble sort, where is the largest value at the end of the first pass?

Quick check+10 XP

Why does the swap in a bubble sort need a variable such as Temp?

Quick check+10 XP

Nums holds 6, 2, 9, 4. What does it hold after one pass of a bubble sort into ascending order?

Section 03

A smarter bubble sort.

Two improvements cut the work: stop as soon as a whole pass makes no swaps, because the array must then be sorted, and make each pass one comparison shorter, because the end of the array is already in order.

DECLARE Top : INTEGER
DECLARE Swapped : BOOLEAN
Top ← 5
REPEAT
    Swapped ← FALSE
    FOR j ← 1 TO Top - 1
        IF Nums[j] > Nums[j + 1] THEN
            Temp ← Nums[j]
            Nums[j] ← Nums[j + 1]
            Nums[j + 1] ← Temp
            Swapped ← TRUE
        ENDIF
    NEXT j
    Top ← Top - 1
UNTIL Swapped = FALSE OR Top = 1
Worked example Trace the improved sort
Nums holds 25, 12, 40, 8, 19. Trace the improved bubble sort pass by pass and count the comparisons.
  1. Pass 1 (Top = 5, j = 1 to 4): 4 comparisons, 3 swaps, giving 12, 25, 8, 19, 40. Top becomes 4.
  2. Pass 2 (j = 1 to 3): 3 comparisons, 2 swaps, giving 12, 8, 19, 25, 40. Top becomes 3.
  3. Pass 3 (j = 1 to 2): 12 > 8 swap, 12 > 19 no: 2 comparisons, giving 8, 12, 19, 25, 40. Top becomes 2.
  4. Pass 4 (j = 1 to 1): 8 > 12 is FALSE, so Swapped stays FALSE; Top becomes 1 and the loop ends.
Answer: Sorted to 8, 12, 19, 25, 40 in 4 + 3 + 2 + 1 = 10 comparisons and 6 swaps (the simple version needs 16 comparisons)
Your turn
The improved sort is run on 3, 5, 6, 8, 9, which is already sorted. How many comparisons does it make?
Examiner trap

Set Swapped ← FALSE at the start of every pass, inside the REPEAT. Setting it once before the loop means a single swap in pass 1 keeps it TRUE for ever.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

A bubble sort uses IF List[j] > List[j + 1] THEN to sort into ascending order. What change sorts into descending order?

Exam style+20 XP

A simple bubble sort uses nested FOR loops that both run from 1 to n − 1. How many comparisons does it make for 6 elements?

Exam style+20 XP

What is an advantage of a linear search?

Exam style+20 XP

In an improved bubble sort, what is the purpose of the Boolean flag?

Skill unlocked

Linear search and bubble sort, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Writing and tracing a linear search and a bubble sort in pseudocode, counting comparisons and improving the bubble sort.