Choose exam board for KS4 Computer Science (GCSE)
Choose exam board for KS4 English
Choose exam board for KS4 French
Choose exam board for KS4 Geography
Choose exam board for KS4 German
Choose exam board for KS4 History
Choose tier for KS4 Maths
Choose exam board for KS4 Music
Choose exam board for KS4 Physical education (GCSE)
Choose exam board for KS4 Religious education (GCSE)
Choose exam board for KS4 Spanish

      Lesson details

      Learning outcome

      I can trace code for a sorting algorithm to identify factors which may impact efficiency.

      Key learning points

      1. Code for sorting algorithms can be analysed by how efficient the algorithm is.
      2. A trace table can be used to analyse an algorithm.
      3. Bubble sort compares elements next to each other in a list and swaps them if they are in the wrong order.
      4. A more efficient version of bubble sort uses a variable to flag when a swap was made during a single pass.
      5. The insertion sort algorithm copies the value to be inserted in a variable at the start of each pass.

      Keywords

      • Trace table - a tool used to track the values of variables and the flow of execution in an algorithm or program, step by step

      • Efficiency - in sorting algorithms, efficiency refers to how effectively an algorithm uses resources (time and space) while sorting a list of data

      Common misconception

      Bubble sort is a fast and efficient sorting algorithm.

      Bubble sort is considered inefficient for large data sets, but it can be made more efficient by using a flag to indicate when a swap is made in a single pass.

      Teacher tip

      This lesson involves tracing through code, it is often easier for pupils to have a paper based version of the trace table to record steps as they go. Versions of the trace tables are available on the worksheets for this lesson.

      Licence

      This content is © Oak National Academy Limited (2025), licensed on Open Government Licence version 3.0
      except where otherwise stated. See Oak's terms & conditions
      (Collection 2).

      Lesson video

      Loading...

      Prior knowledge starter quiz

      6 Questions

      Q1.
      What is the main purpose of a sorting algorithm?

      to delete data from a list
      to find the largest number in a list
      Correct answer: to arrange data in a specific order
      to search for an element in a list

      Q2.
      Which of the following best describes the bubble sort algorithm?

      it divides the list into halves and sorts each half
      it finds the smallest element and places it at the beginning
      it uses a binary tree to sort the data
      Correct answer: it repeatedly swaps adjacent elements if they are in the wrong order

      Q3.
      Arrange the steps of the bubble sort algorithm in the correct order:

      1 - compare adjacent elements
      2 - swap elements if they are in the wrong order
      3 - repeat the process for all elements
      4 - stop when no swaps are needed

      Q4.
      What is a key characteristic of the insertion sort algorithm?

      it always divides the list into two parts
      Correct answer: it builds the sorted list one element at a time
      it only works with numbers
      it requires a pre-sorted list to function

      Q5.
      Arrange the steps of the insertion sort algorithm in the correct order:

      1 - compare the current element with elements in the sorted portion of the list
      2 - shift larger elements in the sorted portion one position to the right
      3 - insert the current element into its correct position in the sorted portion
      4 - repeat for the next element in the unsorted portion until the list is sorted

      Q6.
      Match the following terms with their definitions:

      Correct Answer:bubble sort,repeatedly swaps adjacent elements to sort a list

      repeatedly swaps adjacent elements to sort a list

      Correct Answer:insertion sort,builds the sorted list one element at a time

      builds the sorted list one element at a time

      Correct Answer:linear search,checks each element in a list one by one

      checks each element in a list one by one

      Correct Answer:binary search,divides the list and searches in halves

      divides the list and searches in halves

      6 Questions

      Q1.
      What is the purpose of a trace table in programming?

      to execute the program line by line
      to check for syntax errors in the code
      to optimise the efficiency of an algorithm
      Correct answer: to record the state of variables and conditions during code execution

      Q2.
      Which of the following statements is true about bubble sort?

      it is the most efficient sorting algorithm for large data sets
      Correct answer: it compares adjacent elements and swaps them if needed
      it inserts elements into their correct position one by one
      it uses a divide-and-conquer approach

      Q3.
      How can the performance of bubble sort be improved?

      by sorting only half the list each time
      by using a binary search to find swaps
      Correct answer: by using a variable to flag when a swap is made during a pass
      by increasing the number of comparisons per pass

      Q4.
      What does the efficiency of a sorting algorithm refer to?

      how user-friendly the algorithm is
      Correct answer: how effectively it uses resources such as time and space
      how easy the algorithm is to code
      how many lines of code the algorithm contains

      Q5.
      Arrange the steps of the bubble sort algorithm with a flag in the correct order:

      1 - compare adjacent elements
      2 - swap elements if they are in the wrong order
      3 - set a flag if a swap is made during the pass
      4 - stop if no swaps are made during a pass

      Q6.
      Which sorting algorithm is better for small or partially sorted datasets?

      bubble sort
      Correct answer: insertion sort
      merge sort
      quick sort

      To help you plan your 10 computer science lesson on: Coding sorting algorithms, download all teaching resources for free and adapt to suit your pupils' needs...