Harmony Virtual Lab

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.

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

  1. Choose the algorithm and the list size, then press New list.
  2. Press Step to watch each comparison (yellow) and swap (red).
  3. Press Run to end to finish the sort.
  4. Record the number of comparisons and swaps.
  5. 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

  1. In bubble sort, each step compares

    1. the first and last items
    2. every item with the largest
    3. two neighbouring items
    4. random items
    Show the answer

    C. two neighbouring items Neighbours are compared and swapped if they are in the wrong order.

  2. After the first pass of bubble sort (smallest to largest), the item at the end of the list is

    1. the largest
    2. the smallest
    3. the middle one
    4. unchanged
    Show the answer

    A. the largest The largest item is carried along to the end.

  3. Selection sort works by

    1. swapping neighbours
    2. repeatedly finding the smallest remaining item and moving it to the front
    3. splitting the list in half
    4. 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.

Do the experiment and the full quiz

Related Digital Technologies experiments

  • Logic gates and truth tablesDigital Technologies · SSS 1 – SSS 2Switch the inputs of AND, OR, NOT, NAND, NOR and XOR gates and build each truth table.
  • Binary, octal and hexadecimalDigital Technologies · JSS 2 – SSS 1Flip eight bit-switches to see how computers store numbers, then hit target values in binary.
  • Programming a robot: sequence and loopsDigital Technologies · JSS 1 – SSS 1Write a program of moves and turns to steer a robot to its goal, then shorten it with a loop.
  • How strong is a password?Digital Technologies · JSS 1 – SSS 2Test made-up passwords, measure their strength in bits and see how long an attacker would need to guess them.
  • Spreadsheet formulas and functionsDigital Technologies · JSS 2 – SSS 2Use cell references, SUM, AVERAGE, MAX and IF to complete a shop sales record and a class result sheet.
  • Coding in PythonDigital Technologies · SSS 1 – SSS 3Write real Python – print, variables, loops and if statements – and have each program checked automatically.