← All tools Search & Sorting Algorithms – Part 2

Search & Sorting Algorithms – Part 1

How computers find and order data: Binary Search, Linear Search, and Bubble Sort — with step-by-step animated walkthroughs and a 10-question quiz.

Quick Overview

The essentials for each algorithm, in your own words.

Binary Search

Binary search finds an item in a sorted list by repeatedly checking the middle value. If the target is smaller, search the left half; if larger, search the right half — and repeat until it's found or the list runs out.

Needs a sorted list Worst case: O(log n)

Advantages

  • Very fast for large sorted lists
  • Efficient — O(log n)

Disadvantages

  • Only works on sorted lists
  • More complex to implement

Linear Search

Linear search checks each item in a list one by one, from the start, until it finds the target or reaches the end of the list. It's simple, but slower than binary search on large lists.

Works on unsorted lists Worst case: O(n)

Advantages

  • Simple to understand and use
  • Works on unsorted lists

Disadvantages

  • Slow for large lists — O(n)
  • Inefficient compared to binary search

Bubble Sort

Bubble sort arranges items into order by repeatedly comparing adjacent pairs and swapping them if they're in the wrong order. Each full pass "bubbles" the largest remaining item to the end, until no swaps are needed.

Works with numbers or letters Worst case: O(n²)

Advantages

  • Easy to understand
  • Good for small or nearly-sorted lists

Disadvantages

  • Very slow for large lists — O(n²)
  • Not efficient for real-world use

Binary Search — Step by Step

Watch how the search space is halved each time as it looks for 23 in a sorted list.

low high mid
Show pseudocode
SET low TO 0
SET high TO length of list - 1
WHILE low <= high DO
    SET mid TO (low + high) / 2
    IF list[mid] == target THEN
        PRINT "Item found at position", mid
        EXIT loop
    ELSE IF list[mid] < target THEN
        SET low TO mid + 1
    ELSE
        SET high TO mid - 1
    END IF
END WHILE
IF low > high THEN
    PRINT "Item not found"
END IF

Linear Search — Step by Step

Watch how each item is checked in turn while looking for 20 in an unsorted list.

current
Show pseudocode
SET found TO false
FOR each item IN list
    IF item == target THEN
        PRINT "Item found"
        SET found TO true
        EXIT loop
    END IF
END FOR
IF found == false THEN
    PRINT "Item not found"
END IF

Bubble Sort — Step by Step

Watch adjacent pairs get compared and swapped, pass by pass, until the list is sorted.

Show pseudocode
FOR i FROM 0 TO length of list - 1
    FOR j FROM 0 TO length of list - i - 2
        IF list[j] > list[j + 1] THEN
            SWAP list[j] AND list[j + 1]
        END IF
    END FOR
END FOR

Test Yourself — 10 GCSE-Style Questions

OCR-style multiple choice questions on binary search, linear search and bubble sort.