The Bubble Sort Dance Paul Curzon Queen Mary
The Bubble Sort Dance Paul Curzon Queen Mary University of London CAS London www. cs 4 fn. org www. teachinglondoncomputing. org Twitter: @cs 4 fn
Aims • We are going to explore the way computers sort data • We will also see how dances and programs have a lot in common
Code = Dance • Choreographing a dance is very similar to writing code. • You have to come up with the right steps in the right order. • Once you have created a dance anyone can follow the instructions without having to invent the dance anew. • The dancers follow the steps of the dance just like a computer follows the steps of a program • Programmers choreograph the way data dances round a computer memory.
Sort Algorithms • A sort algorithm takes a series of things and puts it into order • eg • [5, 7, 2, 99, 4] -> [2, 4, 5, 7, 99] • [“cat”, “hat”, “ant”] -> [“ant”, “cat”, “hat”] • Often used as a way of making things easier to find (eg putting your music collection in alphabetical order so you can find tracks).
The Sorting Dance • We are going to choreograph a sort algorithm as though it were a dance. • • We need 5 volunteers to be our dancers • They each get a different number on their back • They start by standing in a line.
The sorting dance (1) The following is the basic steps that make up the dance Pass hat to first dancer Dancer with hat and next dancer step forward if dancer with hat > next dancer Dancer with hat swaps places with next dancer else Dancer with hat passes it to next dancer The pair step back in to the line
The sorting dance (2) We need to do that for each pair, down the line: Pass hat to first dancer Repeat 4 times { Dancer with hat and next dancer step forward if dancer with hat > next dancer Dancer with hat swaps places with next dancer else Dancer with hat passes them to next dancer The pair step back in to the line }
The sorting dance (3) Doing one pass isn’t enough we need to do it over and over again Repeat 4 times { Pass hat to first dancer Repeat 4 times { Dancer with hat and next dancer step forward if dancer with hat > next dancer Dancer with hat swaps places with next dancer else Dancer with hat passes them to next dancer The pair step back in to the line } }
How many passes • How many passes do we need to do to guarantee the dancers are in sorted order? • We have to be sure it works even if everyone starts in the worst possible positions.
How many passes • On the first pass, the biggest value has ended up in the right place • It went with the hat where ever it started. • On the next pass the next biggest is in the right place…and so on • When the second last one is in the right place there is no where else for the last dancer to go so it is right too. • So if there are 5 dancers we will need 4 passes or more generally n dancers needs n-1 passes
Can we do better? • This is quite a simple version of bubble sort • It is possible to come up with an improved version that involves less steps • so the dance is much shorter • Can you work out how?
Doing better still • What happens if the array is already sorted? • Over and over again we do comparisons, lots of wasted dance steps never changing anything
Doing better still Observation • If we do a whole pass and nothing changes then it never will - the array is sorted • So we can shorten the dance further by keeping track of whether we swap anything on a pass and stopping at the end of any pass where none swapped. • Now we have a dance that will be a different length depending on where the numbers are at the start.
Summary • We have seen one simple way to sort data. • We have also seen how writing programs is similar to choreographing a dance. • Both need lots of creativity.
More on the fun side of computer science from: www. cs 4 fn. org www. teachinglondoncomputing. org Twitter: @cs 4 fn With support from Google, BCS, Dept for Education, the Mayor of London and the BBC
- Slides: 15