Sorting Algorithms & BBC Micro
🧮 Understanding Sorting Algorithms and Acorn Computers
This document explores various sorting algorithms and provides insights into the Acorn computer architecture, particularly focusing on the BBC Micro's descendants and their programming capabilities using BBC Basic. It also delves into the process of visualizing and sonifying sorting algorithms.
💻 The Acorn A5000 and BBC Basic
The Acorn A5000, dating from approximately 1991, is a direct descendant of the original BBC Micro. This computer features an ARM chip, a significant upgrade from its predecessor.
Key characteristics of the Acorn A5000:
ARM processor: A more advanced processor compared to the BBC Micro.
Built-in BBC Basic interpreter: Allows for direct programming in BBC Basic.
ROM-based operating system: Enables extremely fast boot-up times, similar to the BBC Micro.
Graphical User Interface (GUI): Unlike the BBC Micro, the A5000 boots directly into a GUI, featuring a mouse and clickable icons.
Icon bar: A navigation bar at the bottom of the screen, providing access to system functions.
Hard drive and floppy drive access: Icons allow users to open and view the contents of storage devices.
"Apps": A term used by Acorn in the 1990s for built-in applications, such as an alarm clock or configuration settings.
The Acorn A5000 represents an evolution in personal computing, bridging the gap between early home computers and modern GUI-based systems, while retaining the educational programming focus of the BBC Micro.
⚙️ Configuring Sound Options
The A5000's configuration settings allow users to change various system options, including sound options. This feature can alter the noises played by the system, which is particularly relevant for the sonification of algorithms.
🎵 Sonifying Sorting Algorithms
The concept of sonifying sorting algorithms involves playing a sound for each comparison or swap of elements during the sorting process. The pitch of the sound can be related to the value of the elements being manipulated, creating an auditory representation of the sorting progress.
💡 Inspiration and Implementation
The idea for sonifying sorting algorithms was inspired by other Cprograms that visualized sorting. However, an inability to get an updated version of a specific C library led to the decision to implement this functionality using BBC Basic on an Acorn machine.
Libraries: Collections of pre-written code that perform specific tasks, often used to save development time and ensure correctness.
BBC Basic implementation: Leveraging existing knowledge of BBC Basic graphics and sound capabilities, a custom implementation was developed.
📝 BBC Basic Code Example: Triangles and Sound
A simple BBC Basic program demonstrates how to draw graphics and play sounds.
MODE 27
REPEAT
COLOR = RND(8) - 1
GCOL COLOR
SOUND 1, -15, COLOR * 32, 5
PLOT RND(100), RND(100), RND(100), RND(100), RND(100), RND(100)
REM FOR I = 1 TO 100 : NEXT I
UNTIL FALSEExplanation of the code:
MODE 27: Sets the screen resolution and available colors.REPEAT ... UNTIL FALSE: Creates an infinite loop, asFALSEnever evaluates toTRUE.COLOR = RND(8) - 1: Assigns a random number (0-7) to theCOLORvariable.GCOL COLOR: Sets the graphics color to the random number.SOUND 1, -15, COLOR * 32, 5: Plays a sound.Parameter 1 (Channel):
1specifies the sound channel.Parameter 2 (Volume):
-15is the loudest (0 is quietest), ranging from 0 to -15.Parameter 3 (Pitch):
COLOR * 32scales the random color (0-7) to a pitch value between 0 and 255, where 0 is the lowest pitch and 255 is the highest.Parameter 4 (Duration):
5specifies the note duration in centiseconds (5 hundredths of a second).
PLOT ...: Draws a triangle on the screen using random coordinates.REM FOR I = 1 TO 100 : NEXT I: This line, when uncommented, introduces a delay to slow down the execution. Without it, the program runs too fast for the sounds to be distinguishable.REMstands for reminder and is used for comments.
📖 Learning Sound Programming from "Games for your BBC Micro"
A book titled "Games for your BBC Micro" served as a resource for learning about sound programming.
Dr. Audio game: A variation of "Dr. Watson" where feedback for guessing a number is given as a tone.
Pitch variation: The pitch of the tone indicates how close the guess is to the correct number.
Line 130: The specific line of code
SOUND 1, -15, ABS(A - B), 5from the game provided the necessary syntax for sound commands.ABS(A - B): Calculates the absolute difference between two numbers, used to determine the pitch based on the accuracy of the guess.
📊 Visualizing and Comparing Sorting Algorithms
The project aims to visualize and sonify several sorting algorithms side-by-side, sorting 100 elements to compare their performance. Each column in the visualization represents an element, with its height indicating its value (low numbers at one end, high numbers at the other).
Visual cues:
Green elements: In their correct, sorted position.
Red elements: Unsorted.
Blue elements: Currently active or being compared/swapped.
Auditory cues: Each swap plays a sound, with fluctuating pitches reflecting the values being exchanged.
1. Bubble Sort 🫧
Bubble Sort is a relatively simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
Process: Elements are continuously swapped until the largest elements "bubble" to the end of the list.
Time Complexity: O(n2) (quadratic) in the worst and average cases.
Performance example:
30 elements: Takes approximately 23.38 seconds, with 384 comparisons and 228 swaps.
60 elements: Doubling the input size to 60 elements results in roughly four times the execution time, comparisons, and swaps, confirming its O(n2) complexity (22=4).
Disadvantage: It is generally considered slow for larger datasets due to its quadratic time complexity.
2. Quick Sort ⚡
Quick Sort is a highly efficient, comparison-based sorting algorithm that uses a divide-and-conquer strategy.
Process:
**Pivot selection**: Chooses an element from the array, called the **pivot**.
**Partitioning**: Rearranges the array such that all elements less than the pivot come before it, and all elements greater than the pivot come after it.
**Recursion**: Recursively applies the same process to the sub-arrays on either side of the pivot.
Time Complexity: O(nlogn) on average, but O(n2) in the worst case (though rare with good pivot selection).
Performance: Considerably faster than bubble sort. For effective speed tests, graphics should be turned off as they significantly slow down execution.
3. Selection Sort 🎯
Selection Sort is another simple, comparison-based sorting algorithm.
Process:
Finds the **minimum element** from the unsorted part of the list.
Swaps it with the element at the beginning of the unsorted part.
Repeats this process until the entire list is sorted.
Time Complexity: O(n2) (quadratic) in all cases.
Performance:
Similar to Bubble Sort in terms of time complexity.
It performs fewer swaps (at most one per element) compared to bubble sort, but the number of comparisons remains high, making it relatively slow.
4. Heap Sort 🌳
Heap Sort is an efficient, comparison-based sorting algorithm that utilizes a special tree-based data structure called a heap.
Heap Data Structure: A binary tree where every parent node's value is greater than or equal to the values of its children (for a max-heap).
Requires understanding of trees and linked lists, which are types of data structures.
Process:
**Build Heap**: Transforms the input array into a max-heap, where the largest element is at the root (index 0).
**Extract Max**: Swaps the largest element (root) with the last element of the heap and reduces the heap size.
**Heapify**: Restructures the remaining elements to maintain the heap property.
Repeats steps 2 and 3 until the heap is empty, resulting in a sorted array.
Time Complexity: O(nlogn) in all cases (worst, average, and best).
Performance: Like Quick Sort, Heap Sort is very quick and efficient for larger datasets.
It's important to distinguish between a "heap" (a data structure) and "the heap" (a region of memory, distinct from the stack).
🚀 Comparative Performance Summary
Algorithm | Time Complexity (Average) | Swaps per Element (Max) | Relative Speed | Notes |
|---|---|---|---|---|
Bubble Sort | O(n2) | Many | Slow | Simple, but inefficient for large datasets. |
Selection Sort | O(n2) | 1 | Slow | Fewer swaps than Bubble Sort, but high comparisons. |
Quick Sort | O(nlogn) | Variable | Fast | Efficient, but worst-case O(n2) is possible. |
Heap Sort | O(nlogn) | Variable | Fast | Guaranteed O(nlogn), uses a heap data structure. |
Bubble Sort, Cocktail Sort, and Selection Sort are significantly slower (O(n2)) compared to Quick Sort and Heap Sort (O(nlogn)), especially as the number of elements increases. The visualizations clearly demonstrate these performance differences.

Ask anything
We've read this whole study set, so questions about it get the best answers.