New
New
Year 10
OCR

Coding sorting algorithms

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

New
New
Year 10
OCR

Coding sorting algorithms

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

These resources will be removed by end of Summer Term 2025.

Switch to our new teaching resources now - designed by teachers and leading subject experts, and tested in classrooms.

These resources were created for remote use during the pandemic and are not designed for classroom teaching.

Lesson details

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 - an error checking method that steps through each line of code in a program and records the state of the variables and conditions

  • 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.


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

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.
Teacher tip

Equipment

This lesson models how sorting algorithms are coded, it may be useful to have devices connected to the internet and access to the Raspberry Pi Foundation's Code Editor to test and run code.

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...

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