Digital Technologies · Algorithms: sorting
Sorting algorithms: bubble sort and selection sort
Step through bubble sort and selection sort on a list of numbers, count comparisons and swaps, and see how the work grows with the size of the list.
- Curriculum: Digital Technologies, Algorithms: sorting
- Class: JSS 2 – SSS 1
- For BECE, WAEC, NECO practicals
- Readings, table
- Quiz with answers explained
Start this experimentAll Digital Technologies experiments
Aim
To follow two sorting algorithms step by step and compare how much work each does.
Apparatus
- Simulated list of numbers (cards)
- Step and Run buttons
- Comparison and swap counters
Theory
An algorithm is a set of exact steps for solving a problem. Sorting puts items in order.
Bubble sort: go through the list comparing neighbours and swap them if they are in the wrong order. After each pass the largest item has “bubbled” to the end. Stop when a pass makes no swaps.
Selection sort: find the smallest item and swap it to the front; then find the smallest of the rest, and so on.
For a list of n items both make about n²/2 comparisons, so doubling the list roughly quadruples the work. Selection sort makes far fewer swaps.
Procedure
- Choose the algorithm and the list size, then press New list.
- Press Step to watch each comparison (yellow) and swap (red).
- Press Run to end to finish the sort.
- Record the number of comparisons and swaps.
- Repeat for the other algorithm and for bigger lists.
Precautions
No hazards – but in real programs, choose an efficient algorithm for big data.
What you do in the virtual lab
Set up the apparatus on screen, take readings and record them in a table (Algorithm; Items (n); Comparisons; Swaps). Simulated instruments give slightly different readings each time, as real ones do. Finish with a short quiz; every answer is explained.
Teachers can set the experiment for a class, see each student’s results and use it as a WAEC- or NECO-style practical paper in exam practice.
Practice questions
In bubble sort, each step compares
- the first and last items
- every item with the largest
- two neighbouring items
- random items
Show the answer
C. two neighbouring items Neighbours are compared and swapped if they are in the wrong order.
After the first pass of bubble sort (smallest to largest), the item at the end of the list is
- the largest
- the smallest
- the middle one
- unchanged
Show the answer
A. the largest The largest item is carried along to the end.
Selection sort works by
- swapping neighbours
- repeatedly finding the smallest remaining item and moving it to the front
- splitting the list in half
- guessing
Show the answer
B. repeatedly finding the smallest remaining item and moving it to the front Each pass selects the minimum of the unsorted part.
Related Digital Technologies experiments
- Logic gates and truth tablesSwitch the inputs of AND, OR, NOT, NAND, NOR and XOR gates and build each truth table.
- Binary, octal and hexadecimalFlip eight bit-switches to see how computers store numbers, then hit target values in binary.
- Programming a robot: sequence and loopsWrite a program of moves and turns to steer a robot to its goal, then shorten it with a loop.
- How strong is a password?Test made-up passwords, measure their strength in bits and see how long an attacker would need to guess them.
- Spreadsheet formulas and functionsUse cell references, SUM, AVERAGE, MAX and IF to complete a shop sales record and a class result sheet.
- Coding in PythonWrite real Python – print, variables, loops and if statements – and have each program checked automatically.