Why Searching and Sorting Matter

Two of the most common tasks in computing are searching (finding a specific item in a collection of data) and sorting (arranging data into a particular order). These operations happen billions of times every day — every Google search, every online shopping filter, every playlist sorted by artist name.

Simple Way to Remember Linear Search = Looking through a messy drawer one item at a time until you find your keys. Binary Search = Playing ‘higher or lower’ to guess a number – you eliminate half the options each time!

There are different algorithms for searching and sorting, and they are not all equally good. Some are simple but slow. Others are clever and fast, but more complex. Understanding the strengths and weaknesses of each algorithm is a key part of GCSE Computer Science.

In this topic, you will learn:

Key Concept An efficient algorithm uses fewer steps to get the same result. When data sets are small, the difference barely matters. But when you are dealing with thousands or millions of items, choosing the right algorithm can mean the difference between a result in one second and a result that takes hours.

Searching and Sorting in Everyday Life

Searching: Finding What You Need

Imagine you have lost your phone somewhere in your bedroom. You could search in two ways:

Sorting: Getting Things in Order

Think about sorting a hand of playing cards:

Think About It: Which method do you naturally use when sorting a hand of cards? Most people use insertion sort without realising it! Try using bubble sort next time and see how it feels different.

Algorithms in Detail

Searching Algorithms

A searching algorithm finds a specific item within a collection of data. You need to know two:

Linear Search

Pseudocode — Linear Search
INPUT searchItem
FOR i = 0 TO LENGTH(list) - 1
    IF list[i] == searchItem THEN
        OUTPUT "Found at position " + i
        STOP
    ENDIF
ENDFOR
OUTPUT "Item not found"

Pros: Works on any list (sorted or unsorted). Simple to understand and implement.

Cons: Slow for large lists — in the worst case, you check every single item.

Binary Search

Binary search is much faster, but the list must be sorted first. It works by repeatedly halving the data:

Pseudocode — Binary Search
INPUT searchItem
SET low TO 0
SET high TO LENGTH(list) - 1
WHILE low <= high
    SET mid TO (low + high) DIV 2
    IF list[mid] == searchItem THEN
        OUTPUT "Found at position " + mid
        STOP
    ELSEIF list[mid] < searchItem THEN
        SET low TO mid + 1
    ELSE
        SET high TO mid - 1
    ENDIF
ENDWHILE
OUTPUT "Item not found"

Pros: Very fast, even with huge lists. Each step eliminates half the remaining data.

Cons: Only works on sorted data. Slightly more complex to implement.

The Guessing Game Analogy Have you ever played ‘guess the number between 1 and 100’? You probably: First guess 50 (the middle). If told ‘too high’, guess 25 (middle of 1–50). If told ‘too low’, guess 37 (middle of 25–50). Keep halving until you find it! That’s exactly how binary search works! You can find ANY number from 1–100 in just 7 guesses maximum.
Common Mistake: Students often forget that binary search requires a sorted list. If the data is not sorted, binary search will not work correctly. If asked which search to use on unsorted data, the answer is always linear search.

Sorting Algorithms

Sorting means arranging data into a particular order (usually ascending or descending). You need to know these sorting algorithms:

Bubble Sort

Pseudocode — Bubble Sort
REPEAT
    SET swapped TO FALSE
    FOR i = 0 TO LENGTH(list) - 2
        IF list[i] > list[i + 1] THEN
            SWAP list[i] AND list[i + 1]
            SET swapped TO TRUE
        ENDIF
    ENDFOR
UNTIL swapped == FALSE

Example walkthrough: Sorting [5, 3, 8, 1]

PassComparisons & SwapsList After Pass
15>3 swap, 5<8 no swap, 8>1 swap[3, 5, 1, 8]
23<5 no swap, 5>1 swap, 5<8 no swap[3, 1, 5, 8]
33>1 swap, 3<5 no swap, 5<8 no swap[1, 3, 5, 8]
4No swaps needed — DONE[1, 3, 5, 8]

Pros: Simple to understand and code. Detects if the list is already sorted (no swaps = stop early).

Cons: Very slow for large lists. Makes many unnecessary comparisons.

Bubble Sort Analogy Think about lining up students by height for a photo. You walk along the line comparing each pair of neighbours. If someone tall is in front of someone short, they swap places. You keep walking through the line until nobody needs to swap.

Insertion Sort

Think about how you sort a hand of playing cards. You pick up one card at a time and slide it into the correct position among the cards you have already sorted.

Pseudocode — Insertion Sort
FOR i = 1 TO LENGTH(list) - 1
    SET current TO list[i]
    SET j TO i - 1
    WHILE j >= 0 AND list[j] > current
        SET list[j + 1] TO list[j]
        SET j TO j - 1
    ENDWHILE
    SET list[j + 1] TO current
ENDFOR

Example walkthrough: Sorting [5, 3, 8, 1]

StepCurrent ItemActionList After Step
133 < 5, so shift 5 right and insert 3 before it[3, 5, 8, 1]
288 > 5, so no shift needed — stays in place[3, 5, 8, 1]
311 < 8, shift 8 right; 1 < 5, shift 5 right; 1 < 3, shift 3 right; insert 1 at start[1, 3, 5, 8]

Pros: Simple to understand. Very efficient for small lists or lists that are already nearly sorted. Uses very little extra memory.

Cons: Slow for large, unsorted lists (similar to bubble sort).

Insertion Sort vs. Bubble Sort Both insertion sort and bubble sort are slow on large data sets, but insertion sort is generally faster in practice because it makes fewer comparisons and swaps, especially when the data is already partially sorted. If a list is nearly sorted, insertion sort can finish very quickly, whereas bubble sort still needs to make full passes through the data.

Merge Sort

Merge sort uses a “divide and conquer” approach. Instead of sorting the whole list at once, you split it in half, then split those halves in half again, and keep going until you have sub-lists of just one item each (which are already “sorted”). Then you merge them back together in the right order.

Pseudocode — Merge Sort
FUNCTION mergeSort(list)
    IF LENGTH(list) <= 1 THEN
        RETURN list
    ENDIF
    SET mid TO LENGTH(list) DIV 2
    SET left TO mergeSort(list[0 TO mid - 1])
    SET right TO mergeSort(list[mid TO end])
    RETURN merge(left, right)
ENDFUNCTION

FUNCTION merge(left, right)
    SET result TO empty list
    WHILE left is not empty AND right is not empty
        IF left[0] <= right[0] THEN
            APPEND left[0] TO result
            REMOVE first item from left
        ELSE
            APPEND right[0] TO result
            REMOVE first item from right
        ENDIF
    ENDWHILE
    APPEND remaining items to result
    RETURN result
ENDFUNCTION

Example walkthrough: Sorting [6, 3, 8, 1]

StepActionResult
Split[6, 3, 8, 1] → [6, 3] and [8, 1]Two halves
Split again[6, 3] → [6] and [3]; [8, 1] → [8] and [1]Four singles
Merge[6] + [3] → [3, 6]; [8] + [1] → [1, 8]Two sorted pairs
Merge[3, 6] + [1, 8] → [1, 3, 6, 8]Fully sorted

Pros: Much faster than bubble sort for large lists. Consistent performance regardless of how the data starts.

Cons: Uses more memory (needs extra space for the sub-lists). More complex to code.

Key Concept: Comparing Sorts Bubble sort is simple but slow — best for small or nearly-sorted lists. Insertion sort is also simple and performs well on small or nearly-sorted data, often better than bubble sort. Merge sort is faster for large data sets but uses more memory. In exams, you may be asked to trace through each algorithm step by step, so practise with small lists until you are confident.

Comparing Algorithms: When to Use Which

When to Use Which — Searching Use Linear Search when data is NOT sorted, you have a small list, or you need simple code. Use Binary Search when data IS sorted and you have a large list. In a phone book with 1 million names, linear search might check all 1 million — binary search finds anyone in just 20 checks!
When to Use Which — Sorting Bubble sort and insertion sort are simple but slow on large lists — best for small data sets or data that is already nearly sorted. Merge sort is much faster for large data sets, but uses more memory because it creates sub-lists during the divide step.

How Many Comparisons?

To understand why some algorithms are faster than others, think about how many comparisons they need for a list of 1,000 items:

AlgorithmComparisons (worst case, 1,000 items)Why?
Linear Search Up to 1,000 Checks every item one by one
Binary Search Up to 10 Halves the data each step (210 = 1,024)
Bubble Sort Up to ~1,000,000 Compares every pair, multiple passes
Merge Sort Up to ~10,000 Divides and merges efficiently

The choice of algorithm makes an enormous difference. For large data sets, a slow algorithm can take hundreds of times longer than a fast one to produce the same result.

Test Yourself

Click on each question to reveal the answer. Try to answer in your head first!

Q1: What is the main requirement for binary search that is not needed for linear search?

Answer: Binary search requires the data to be sorted before it can work. Linear search can work on any list, whether sorted or unsorted. This is the most important distinction between the two algorithms.

Q2: A list contains 1,000 items. In the worst case, how many comparisons would a linear search need? How many would a binary search need?

Answer: Linear search: up to 1,000 comparisons (checking every single item). Binary search: up to approximately 10 comparisons (since 210 = 1,024, and each step halves the data). This shows how dramatically more efficient binary search is for large data sets.

Q3: In bubble sort, how do you know the algorithm has finished?

Answer: Bubble sort is complete when a full pass through the list is made with no swaps. This means every adjacent pair is already in the correct order, so the list must be fully sorted.

Q4: What approach does merge sort use, and what does this mean?

Answer: Merge sort uses a divide and conquer approach. This means it repeatedly divides the problem in half (splitting the list into smaller sub-lists) until the sub-problems are trivially small, then conquers by merging the solved sub-problems back together in the correct order.

Q5: Trace through a bubble sort of the list [4, 2, 7, 1]. Show the list after each pass.

Answer:

Original: [4, 2, 7, 1]

Pass 1: Compare 4,2 → swap → [2, 4, 7, 1]. Compare 4,7 → no swap. Compare 7,1 → swap → [2, 4, 1, 7].

Pass 2: Compare 2,4 → no swap. Compare 4,1 → swap → [2, 1, 4, 7]. Compare 4,7 → no swap.

Pass 3: Compare 2,1 → swap → [1, 2, 4, 7]. Compare 2,4 → no swap. Compare 4,7 → no swap.

Pass 4: No swaps → sorted! Final list: [1, 2, 4, 7]

Q6: Trace through a binary search for the value 7 in the sorted list [1, 3, 5, 7, 9, 11, 13]. Show each step.

Answer:

Step 1: low = 0, high = 6, mid = (0 + 6) DIV 2 = 3. list[3] = 7.

7 == 7, so the item is found at position 3.

Binary search found it in just 1 comparison. A linear search starting from position 0 would have needed 4 comparisons (checking 1, 3, 5, then 7).

Q7: Give one advantage and one disadvantage of merge sort compared to bubble sort.

Answer:

Advantage: Merge sort is significantly faster for large data sets. For example, on a list of 10,000 items, merge sort might need roughly 130,000 comparisons while bubble sort could need up to 100,000,000 comparisons.

Disadvantage: Merge sort uses more memory than bubble sort because it creates new sub-lists at each stage of the divide step. Bubble sort sorts the list “in place” without needing significant extra memory.

Q8: Why is binary search faster than linear search for large lists?

Answer: Binary search is faster because it halves the remaining data with each comparison, whereas linear search checks each item one at a time. For a list of 1,000 items, linear search might need up to 1,000 comparisons, but binary search only needs about 10 (since 210 = 1,024). However, binary search only works on sorted data, so if the data is unsorted you must use linear search.

Past Paper Practice

Q9 (Past Paper Style — 4 marks): A sorted list contains these values: [3, 7, 12, 18, 24, 31, 45]. Show the steps a binary search would take to find the value 18.

Answer:

Step 1: low=0, high=6, mid=3. list[3]=18, which equals target.

Result: Found at index 3.

(1 mark for correct method, 1 mark for each correct step, 1 mark for correct answer)

Q10 (Past Paper Style — 4 marks): The following list needs to be sorted using bubble sort: [12, 8, 15, 3, 9]. Show the state after each complete pass.

Answer:

Pass 1: [8, 12, 3, 9, 15]

Pass 2: [8, 3, 9, 12, 15]

Pass 3: [3, 8, 9, 12, 15]

Pass 4: No swaps, sorted.

Key Vocabulary

Make sure you understand and can define all of these terms.

TermDefinition
Linear Search A search algorithm that checks each item in a list one by one from start to end. Works on unsorted data. Simple but slow for large lists.
Binary Search A search algorithm that repeatedly halves a sorted list to find an item. Much faster than linear search, but only works on sorted data.
Bubble Sort A sorting algorithm that repeatedly compares adjacent pairs and swaps them if they are in the wrong order. Simple to understand but slow on large lists.
Insertion Sort A sorting algorithm that builds a sorted list one item at a time by inserting each new item into its correct position. Good for small or nearly-sorted lists.
Merge Sort A sorting algorithm that divides a list in half repeatedly, then merges the halves back in order. Much faster than bubble sort for large lists, but uses more memory.
Efficiency A measure of how well an algorithm performs, usually described in terms of time (speed) and space (memory usage) as the data set grows larger.
Divide and Conquer An approach that breaks a problem into smaller sub-problems, solves each one, and combines the results. Used by merge sort and binary search.

Exam Tips: Search & Sort Questions

1. Trace Tables: Show Your Working

When asked to trace through an algorithm, always use a trace table. Write a column for each variable and a row for each step. This makes your working clear to the examiner, and even if your final answer is wrong, you can still pick up method marks for correct intermediate steps.

2. Always State Advantages AND Disadvantages

When a question asks you to compare two algorithms (e.g. bubble sort vs. merge sort, or linear search vs. binary search), make sure you mention at least one advantage and one disadvantage of each. A one-sided answer will only score half marks at most.

3. Read the Data Carefully

Pay attention to whether data is sorted or unsorted. If a question gives you an unsorted list and asks which search algorithm to use, the answer is linear search — binary search cannot be used on unsorted data. If the data is sorted, mention that binary search would be more efficient.

4. Explain Why One Algorithm Is Faster

You should be able to explain why one algorithm is faster than another. Practise sentences like: “Binary search is faster than linear search because it halves the remaining data with each comparison, whereas linear search checks each item one at a time.” Always link your answer to what the algorithm actually does.

Top Exam Tip: If you are running out of time and cannot finish a trace table, at least write down your final answer with a brief explanation. You may still get the mark for the correct answer, and partial working often picks up method marks. Never leave an algorithm question blank.

Past Paper Questions

Try these exam-style questions, then click to reveal the mark scheme answer.

State one advantage and one disadvantage of binary search compared to linear search. [2] marks

Mark scheme:

  • Advantage: Binary search is faster / more efficient for large data sets / O(log n) vs O(n) (1)
  • Disadvantage: Data must be sorted first before binary search can be used (1)
Describe how a bubble sort algorithm sorts the following list into ascending order: [5, 3, 8, 1]. Show the result after each complete pass. [3] marks

Mark scheme:

  • Pass 1: [3, 5, 1, 8] (1)
  • Pass 2: [3, 1, 5, 8] (1)
  • Pass 3: [1, 3, 5, 8] (1)

(accept minor variations in intermediate steps as long as final result is correct and swapping logic is shown)

Explain why merge sort is more efficient than bubble sort for sorting large data sets. [2] marks

Mark scheme:

  • Merge sort divides the list into halves repeatedly (divide and conquer) (1)
  • Merge sort has time complexity O(n log n) compared to bubble sort's O(n²), making it significantly faster for large data sets (1)

Think About It

Search and sort algorithms are not just exam topics — they are the backbone of the technology you use every day:

The algorithms you have learned in this topic are the foundations that make all of this possible. As data sets get larger and larger in the modern world, the choice of algorithm becomes more and more important.

Interactive Activities

Interactive Games

Further Reading & External Resources

Video Resources

All algorithm videos are embedded next to each algorithm section above. Scroll up to find the video for the specific algorithm you want to revise.