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.
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:
- Two searching algorithms: Linear search and binary search
- Three sorting algorithms: Bubble sort, insertion sort, and merge sort
- How to compare them and when to use which one
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:
- Linear search: Check every single object, one at a time, starting from one corner and working across the room. You will definitely find it, but it might take a while.
- Binary search: If your bedroom was somehow organised alphabetically (A-side to Z-side!), you could jump to the middle and quickly narrow down which half your phone is in. Much faster — but it only works if things are already organised.
Sorting: Getting Things in Order
Think about sorting a hand of playing cards:
- Bubble sort: Compare pairs of adjacent cards and swap if they are in the wrong order. Keep going through the whole hand until no more swaps are needed.
- Insertion sort: Pick up one card at a time and slide it into the correct position among the cards you have already sorted.
- Merge sort: Split your hand in half, split those halves in half, keep going until you have individual cards, then merge them back together in order.
Algorithms in Detail
Searching Algorithms
A searching algorithm finds a specific item within a collection of data. You need to know two:
Linear Search
- Start at the first item in the list
- Compare it to the item you are looking for
- If it matches, you are done
- If not, move to the next item
- Repeat until you find it or reach the end of the list
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:
- The list must be sorted first
- Look at the middle item
- If it matches, you are done
- If your target is smaller, discard the right half
- If your target is larger, discard the left half
- Repeat on the remaining half until found or nothing left
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.
Sorting Algorithms
Sorting means arranging data into a particular order (usually ascending or descending). You need to know these sorting algorithms:
Bubble Sort
- Compare each pair of adjacent items
- If they are in the wrong order, swap them
- Repeat the entire pass through the list
- Stop when a complete pass is made with no swaps
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]
| Pass | Comparisons & Swaps | List After Pass |
|---|---|---|
| 1 | 5>3 swap, 5<8 no swap, 8>1 swap | [3, 5, 1, 8] |
| 2 | 3<5 no swap, 5>1 swap, 5<8 no swap | [3, 1, 5, 8] |
| 3 | 3>1 swap, 3<5 no swap, 5<8 no swap | [1, 3, 5, 8] |
| 4 | No 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.
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.
- Start with the second item in the list (the first item is already “sorted” by itself)
- Compare it with items to its left
- Shift larger items one place to the right to make room
- Insert the current item into its correct position
- Repeat for every remaining item in the list
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]
| Step | Current Item | Action | List After Step |
|---|---|---|---|
| 1 | 3 | 3 < 5, so shift 5 right and insert 3 before it | [3, 5, 8, 1] |
| 2 | 8 | 8 > 5, so no shift needed — stays in place | [3, 5, 8, 1] |
| 3 | 1 | 1 < 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).
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.
- Divide: Split the list in half, repeatedly, until each sub-list has just one item
- Conquer: Merge pairs of sub-lists back together in sorted order
- Keep merging until you have one fully sorted list
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]
| Step | Action | Result |
|---|---|---|
| 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.
Comparing Algorithms: When to Use Which
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:
| Algorithm | Comparisons (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!
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.
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.
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.
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.
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]
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).
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.
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
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)
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.
| Term | Definition |
|---|---|
| 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.
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:
- Google sorts billions of web pages by relevance in under a second
- Spotify searches through millions of songs to find your request
- Amazon sorts products by price, rating, or relevance when you browse
- Your phone’s contacts are kept in sorted order so you can find names quickly
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
- Linear Search Simulator — Step through a linear search algorithm visually and compare efficiency across rounds
- Binary Search Simulator — Interactive binary search with pseudocode highlighting and comparison tracking
- Merge Sort Visualiser — Watch divide-and-conquer in action as arrays split and merge step by step
- Tree Traversal Explorer — Visualise inorder, preorder and postorder traversals with step-by-step animation
Interactive Games
- Array Learning Game — Interactive 1D/2D array practice with Parsons problems
- Array Search Builder — Build 2D array search programs with drag-and-drop challenges
Further Reading & External Resources
- Isaac Computer Science — Searching and Sorting Algorithms — Excellent interactive explanations with step-by-step animations
- W3Schools — Data Structures and Algorithms — Beginner-friendly tutorials with try-it-yourself examples
- BBC Bitesize — Edexcel GCSE Computer Science — Comprehensive coverage of all GCSE algorithm topics
- GCSE Topic 1: Computational Thinking & Algorithms — Full Edexcel specification coverage
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.