Bubble Sort

Bubble sort is a simple sorting technique used by programmers. This video shows you how a bubble sort works. The second video will show you how to code one in Scratch.

 

In a bubble sort you compare the first and second items in the list and swap if the first is less than the second. Then compare the second with the third and again swap if necessary, so the third is the smallest value so far. Repeat to the end of the loop and you know the last value is the smallest.

Do the same again from the start, but stop at the second last value since the last value is in the right place. Repeat, taking one off the number of times through the loop each time, until you know you've got the second number in the right place.

Coding a Bubble Sort

 

Here is the code that was used in this example

Make a Scratch that asks the user to type in ten items for a shopping list. (Store them in a list)

Use a bubble sort to sort the list into alphabetical order.

(Tip: Scratch compares words in the same way as numbers. So 'cat' would be greater than 'ant' because c comes after a in the alphabet.)

When you have finished, SAVE and SHARE it.