Guide 8 of 8 ยท Sorting & Searching

Sorting & Searching
Algorithms

Bubble Sort, Insertion Sort, Linear Search & Binary Search โ€” with exact Cambridge pseudocode and runnable exam examples.

โšก Quick Summary: Algorithms at a Glance

  • Bubble Sort: Compares adjacent elements and swaps them if in the wrong order. O(nยฒ) time complexity. Simple but inefficient for large data.
  • Insertion Sort: Builds a sorted portion of the array one element at a time, inserting each new element in the correct position.
  • Linear Search: Checks every element one by one from the start. Works on unsorted data. O(n) time complexity.
  • Binary Search: Divides sorted data in half repeatedly to locate a target. O(log n) time complexity โ€” much faster but requires sorted data.

1. Bubble Sort Pseudocode

Bubble Sort repeatedly passes through the list, comparing adjacent elements and swapping them if they are in the wrong order. Each pass "bubbles" the largest unsorted element to its correct position at the end.

Bubble Sort โ€” Cambridge Pseudocode
// Array A[1:n] is declared and populated before this
DECLARE i, j, Temp : INTEGER
DECLARE n : INTEGER
n โ† 5

FOR i โ† 1 TO n - 1
    FOR j โ† 1 TO n - i
        IF A[j] > A[j + 1]
            THEN
                // Swap using a temporary variable
                Temp โ† A[j]
                A[j] โ† A[j + 1]
                A[j + 1] โ† Temp
        ENDIF
    NEXT j
NEXT i

2. Insertion Sort Pseudocode

Insertion Sort builds the sorted array one item at a time. It picks each element and inserts it into the correct position relative to the already-sorted elements to its left.

Insertion Sort โ€” Cambridge Pseudocode
DECLARE i, j, Key : INTEGER

FOR i โ† 2 TO n
    Key โ† A[i]
    j โ† i - 1
    WHILE j >= 1 AND A[j] > Key
        A[j + 1] โ† A[j]
        j โ† j - 1
    ENDWHILE
    A[j + 1] โ† Key
NEXT i

Linear Search (also called Sequential Search) checks each element one by one from the beginning until the target is found or the end is reached. It works on both sorted and unsorted arrays.

Linear Search โ€” Cambridge Pseudocode
DECLARE Target, i : INTEGER
DECLARE Found : BOOLEAN
Found โ† FALSE

OUTPUT "Enter the value to search for:"
INPUT Target

FOR i โ† 1 TO n
    IF A[i] = Target
        THEN
            Found โ† TRUE
            OUTPUT "Found at index: ", i
    ENDIF
NEXT i

IF Found = FALSE
    THEN OUTPUT "Value not found."
ENDIF

Binary Search is much more efficient than linear search but requires the array to be sorted first. It works by repeatedly halving the search range, comparing the target to the middle element.

Binary Search โ€” Cambridge Pseudocode
DECLARE Target, Low, High, Mid : INTEGER
DECLARE Found : BOOLEAN

Low โ† 1
High โ† n
Found โ† FALSE

OUTPUT "Enter value to find:"
INPUT Target

WHILE Low <= High AND Found = FALSE
    Mid โ† (Low + High) DIV 2
    IF A[Mid] = Target
        THEN
            Found โ† TRUE
            OUTPUT "Found at index: ", Mid
        ELSE
            IF A[Mid] < Target
                THEN Low โ† Mid + 1
                ELSE High โ† Mid - 1
            ENDIF
    ENDIF
ENDWHILE

IF Found = FALSE
    THEN OUTPUT "Value not found."
ENDIF

Algorithm Comparison Table

Algorithm Time Complexity Needs Sorted Data? Best For
Bubble SortO(nยฒ)NoSmall datasets, teaching
Insertion SortO(nยฒ)NoNearly sorted data
Linear SearchO(n)NoSmall/unsorted data
Binary SearchO(log n)Yes โ€” must be sortedLarge sorted datasets

Interactive Exam Practice

Scenario: Write a Bubble Sort algorithm that sorts an array of 6 names (strings) into alphabetical order and then outputs them.

Hover to reveal the answer. Click Run Code to test it instantly!

Tap to Reveal Answer
DECLARE Names[1:6] : STRING
DECLARE i, j : INTEGER
DECLARE Temp : STRING

Names[1] โ† "Zara"
Names[2] โ† "Alice"
Names[3] โ† "Mike"
Names[4] โ† "Ben"
Names[5] โ† "Quinn"
Names[6] โ† "Emma"

FOR i โ† 1 TO 5
    FOR j โ† 1 TO 6 - i
        IF Names[j] > Names[j + 1]
            THEN
                Temp โ† Names[j]
                Names[j] โ† Names[j + 1]
                Names[j + 1] โ† Temp
        ENDIF
    NEXT j
NEXT i

FOR i โ† 1 TO 6
    OUTPUT Names[i]
NEXT i

Related Guide

โ† Arrays Guide

Full Reference

Cheat Sheet โž”