1. Home
  2. Explore
  3. Computer Science
  4. Sorting Algorithms & BBC Micro

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Sorting Algorithms & BBC Micro

Speaker 1Speaker 2

Sorting Algorithms & BBC Micro

9:43
0:00-9:43
  1. Corey

    Simon, it's great to be back discussing sorting algorithms! Our last few videos generated a lot of comments and requests for more, so I'm excited to dive deeper today. It's been a couple of weeks, and I've been doing some interesting research.

  2. Simon

    Absolutely, Corey. I've also been inspired, especially after watching that Computerphile video on the BBC Micro. It's fascinating how the BBC commissioned Acorn to create that computer to introduce people to programming and computing in a really accessible way.

  3. Corey

    That's right! And it ties into what I've been working on. I'm using an Acorn A5000, which is a descendant of the BBC Micro, dating from around 1991. It even has an ARM chip, unlike the original, and a BBC BASIC interpreter built-in.

  4. Simon

    Oh, that's a brilliant choice! I know there's a BBC BASIC interpreter for Windows, but it can be a bit limited, and frankly, who wants to pay for it when you have such a classic machine at hand? The A5000 must boot up incredibly fast too, with its OS in ROM.

  5. Corey

    It does, just like the Micro. You hit the power button and it's almost instant. Unlike its predecessor though, this one boots into a GUI. You've got a mouse, an icon bar at the bottom, and clickable icons for the hard drive, floppy, and even 'apps'.

  6. Simon

    Apps in the 90s? That's quite forward-thinking for the time! So, it's a full graphical environment then. I imagine that makes navigating and running programs a bit more intuitive than a command-line interface, even for BASIC programming.

  7. Corey

    Exactly. 'Apps' is just short for applications. You click it, and there are built-in utilities like an alarm. You can also dive into the configuration to change things like sound options, which is particularly relevant to what I've been experimenting with.

  8. Simon

    Sound options, you say? That's interesting. I recall seeing some C++ visualizations of sorting algorithms online where they'd play a pitch based on an element's value during a swap. Is that what you're getting at with your A5000?

  9. Corey

    Precisely! I saw those videos and thought it was a fantastic idea, but they hadn't covered all the sorting algorithms I wanted to explore. I decided to implement it myself. Their C++ source code used a library I couldn't update, so I turned to my Acorn.

  10. Simon

    That's a classic programmer's journey: see something cool, want to expand on it, hit a compatibility snag, and then just build it yourself from scratch. Libraries are certainly helpful, but sometimes rolling your own solution is the best way to learn and customize.

  11. Corey

    Definitely. I remembered that BBC BASIC video and the triangles program on screen. I thought, if I can draw triangles, I can probably visualize sorting. And yes, I wanted to add sound to it. I'll show you the code for the triangles program as a starting point.

  12. Simon

    I'd love to see it. It's always fascinating to peek under the hood of these older systems and see how things were done. I imagine the code is quite concise, given the limitations of the era, but also very direct in its commands.

  13. Corey

    It is. Here's a snippet: MODE 27 sets the screen resolution and colors. Then, there's a REPEAT UNTIL FALSE block, which means it runs forever. Inside, it sets a variable 'color' to a random number between 0 and 7 and sets the graphics color to that.

  14. Simon

    So, random colors every iteration. And the sound? How does that integrate with the random numbers? It sounds like you're linking the visual and auditory feedback, which is a great way to make the sorting process more engaging and understandable.

  15. Corey

    Exactly. We PLAY a sound related to that random number. The numbers 0-7 are scaled to 0-255 for the pitch value, where 0 is the lowest pitch and 255 is the highest. If we didn't scale it, all the sounds would be very low and too similar.

  16. Simon

    That scaling is crucial for discernible differences. What about the drawing itself? Is there a simple command for that, or is it a series of line drawing instructions to create the triangles on the screen?

  17. Corey

    A single line, actually: PLOT, which draws the triangle. Now, an important line here is one that slows it down. If I comment it out with REM, which stands for 'reminder,' the program runs so fast you can't hear any sound because it's constantly starting and stopping new pitches.

  18. Simon

    That's a common issue with fast processing and audio output on older systems; the sounds can just become a blur if they're not given enough time. So, that pause is essential for the auditory feedback to be meaningful. Where did you learn these sound commands?

  19. Corey

    Good question! I actually got my hands on a book called 'Games for your BBC Micro'. It had code listings, and one game, 'Dr. Audio,' gave feedback as a tone, with the pitch varying by how far your guess was from the correct number. Line 130 had the sound command.

  20. Simon

    A physical book for code listings! That's a delightful throwback. So, it basically taught you the syntax for generating sounds through practical examples. What did that sound command look like in BBC BASIC, and what do all its parameters mean?

  21. Corey

    It was `SOUND 1, -15, ABS(A - B), 5`. I had to look it up as the book didn't fully explain it. The first parameter is the channel, say channel 1. The second is volume, from 0 to -15, where -15 is loudest. ABS(A-B) gets the absolute difference for the pitch.

  22. Simon

    Ah, the absolute difference for pitch makes perfect sense for a 'hotter/colder' game. And the final parameter is for duration, I assume? It's fascinating how concise these commands are, packing a lot of functionality into just a few values.

  23. Corey

    Yes, the last parameter is duration in centiseconds, so `5` means 0.05 seconds. I modified Dr. Audio slightly; my version plays a higher pitch the closer your guess is. Let's try it out! I'm thinking of a number between 1 and 100. My first guess is 25.

  24. Simon

    Okay, 25... and that pitch suggests it's quite far off. My turn. Let's try 75. Oh, that's definitely closer! The pitch is higher. How about 80? No, that's further away. So, back to 70 then. Hooray, that sounds like a winner!

  25. Corey

    You got it! But you know, back then, there wasn't a disk with that book. The 'data' was literally on the pages. I had to type every single line of code in manually, which is a completely different experience from today's copy-paste world.

  26. Simon

    That's an incredible amount of dedication! Typing everything in would certainly make you intimately familiar with every line of code. But it also means you're probably very good at spotting typos, which is a vital skill for debugging.

  27. Corey

    Exactly. After getting familiar with BBC BASIC, drawing, and sounds, I felt ready to implement those visual sorting algorithms. First up, bubble sort. I've got it set to sort 30 elements here, visualizing them as columns.

  28. Simon

    Bubble sort, the classic. I remember how it works: comparing adjacent elements and swapping them if they're out of order, gradually 'bubbling' the largest elements to the end. How do you visualize the state of the elements?

  29. Corey

    Green elements are in their correct sorted position, red ones are unsorted, and blue ones are currently active, meaning they're being compared. For 30 elements, it took 23.38 seconds, 384 comparisons, and 228 swaps. You can hear the fluctuating pitches with each swap.

  30. Simon

    Wow, over 23 seconds for just 30 elements! And the sound really helps to highlight each individual comparison and swap. What happens if you increase the input size, say to 60 elements? Bubble sort has an N-squared complexity, right?

  31. Corey

    That's right. If I change MAX_FILE from 30 to 60, we've doubled the input. Because it's N-squared, it should take roughly four times as long, with four times the comparisons and swaps. Each time it compares or swaps, it increments a counter you can see printed on screen.

  32. Simon

    So, doubling the elements makes it four times slower. That's a stark illustration of N-squared complexity. The visual representation of the columns shifting and changing color, combined with the sounds of the swaps, really brings the algorithm to life.

  33. Corey

    It's a good way to show how inefficient it can be for larger datasets. The columns represent numbers; low numbers at one end, high at the other. When unsorted, they're all mixed, and then they slowly organize into order. It's quite satisfying to watch, albeit slow.

  34. Simon

    It certainly makes the 'bubbling' effect clear. We've also covered quicksort in previous videos, which is known for being much faster. Can you show us quicksort on the A5000, and how does it compare visually and audibly?

  35. Corey

    Of course. I'll reduce the number of elements from 1000 to, say, 400 for demonstration. I also have an option to turn graphics on or off. For true speed tests, you'd turn graphics off, but for visualization, we need them on. Let's run it.

  36. Simon

    Okay, 400 unsorted elements displayed. From our previous video, I remember quicksort works by picking a 'pivot' element, partitioning the list so smaller elements are on one side and larger on the other, then recursively sorting those sub-lists.

  37. Corey

    Exactly. You can see it working its way through the left-hand side, then it will start on the right. It keeps picking pivots and partitioning until everything is sorted. It's dramatically quicker than bubble sort, even with graphics enabled.

  38. Simon

    The speed difference is immediately apparent. It's much more dynamic, the elements moving into place far more rapidly. It really highlights why quicksort is preferred for larger datasets over something like bubble sort.

  39. Corey

    It truly is. Another algorithm we haven't covered before but is quite straightforward is selection sort. Here, you iterate through the unsorted list, find the smallest element, and move it to the beginning of your sorted section.

  40. Simon

    So you repeatedly scan for the minimum and place it. How does selection sort compare in terms of speed and complexity to bubble sort or quicksort? Is it also an N-squared algorithm?

  41. Corey

    Yes, it's also N-squared because you have to repeatedly scan the remaining unsorted list. Here it is sorting 100 elements. You can see it looking for the smallest and then moving it to the left. While it's N-squared, it often performs a bit better than bubble sort.

  42. Simon

    Better than bubble sort, you say? What makes it perform slightly better if they share the same time complexity? Is it about the number of swaps or comparisons it has to make during the sorting process?

  43. Corey

    It definitely does fewer swaps. Selection sort only needs at most one swap per element in the list, whereas bubble sort can go quite wild with swaps. However, the number of comparisons is still high, which contributes to its overall slowness compared to something like quicksort.

  44. Simon

    That makes sense. Swaps can be expensive operations, so minimizing those is a definite win. I'm keen to see all these in action, side by side, maybe sped up a bit, for a direct comparison of their visual behavior and efficiency.

  45. Corey

    That's exactly what I've set up! We've seen bubble, cocktail, and selection sort are quite slow compared to quicksort. Quick sort runs in N log N time, which is much more efficient for larger datasets. But there was another algorithm people asked for.

  46. Simon

    Ah yes, Heap Sort! That was a popular request in the comments. I remember it also runs in N log N, but it relies on a specific data structure. We haven't really delved into data structures like trees or linked lists yet, which are crucial for understanding heaps.

  47. Corey

    You're right, understanding trees and linked lists is fundamental to a proper dive into heaps. A heap is a binary tree where every parent node has children with smaller values. We haven't covered those prerequisites fully, but I can still show you Heap Sort in action.

  48. Simon

    That would be fantastic! Even without the deep dive into the underlying data structures, seeing it work visually will give us a good intuition. How does Heap Sort generally operate then, at a high level, for sorting?

  49. Corey

    It first builds a heap, where the largest element is always at the 'root' or index zero. Then, it swaps that largest element to the end of the array, effectively moving it to its sorted position, and rebuilds the heap with the remaining unsorted elements. It repeats this.

  50. Simon

    So, it repeatedly extracts the maximum element and places it in its correct sorted position. Let's see it go! Building the heap first, then extracting and restructuring. This sounds like an elegant approach once the heap is established.

  51. Corey

    Here it goes. You can see it building the heap now. Once built, it starts taking the largest element, putting it in place, and maintaining that heap structure, producing the sorted list. This was 100 elements, same as the others.

  52. Simon

    That was incredibly quick for 100 elements, much more in line with quicksort's performance. It really underscores the efficiency of N log N algorithms compared to N-squared. And just to clarify, this 'heap' is a data structure, not the memory area, right?

  53. Corey

    Absolutely, it's a data structure, distinct from the 'heap' as in the memory region, which is often contrasted with the stack. They're both called 'heap,' which can be confusing, but they refer to entirely different concepts in computer science.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent