Bubble Sort Exercise

Take 10 number cards of the same suit from a standard deck of playing cards and shuffle them into a random order.  Lay the randomised cards out in front of you, in a row.

heartsrandon

Your goal is to sort the cards into ascending numerical order as shown below, using a bubble sort.

heartsnumbers

Bubble sort the cards as follows:

  • Start from the left
  • Compare cards 1 and 2.  If card 1 is bigger than card 2, swap their positions, otherwise leave them alone.
  • Compare cards 2 and 3. If card 2 is bigger than card 3, swap their positions, otherwise leave them alone.
  • Compare cards 3 and 4. If card 3 is bigger than card 4, swap their positions, otherwise leave them alone.
  • Repeat this process until you reach the end of the row (The biggest number should have bubbled up to the right hand side of the list).
  • Now repeat the WHOLE PROCESS again, starting from the left hand side of the list.

Ask a partner to shuffle your cards and lay them out in front of you again.  Bubble sort them again (and again if necessary), until you are confident with the process.

Do it again and record the swaps in a table similar to the one below.

7 3 1 8 2 6 10 4 5 9 swap 7 and 3
3 7 1 8 2 6 10 4 5 9 swap 7 and 1
3 1 7 8 2 6 10 4 5 9 no swap
3 1 7 8 2 6 10 4 5 9 swap 8 and 2
3 1 7 2 8 6 10 4 5 9 swap 8 and 6
3 1 7 2 6 8 10 4 5 9 no swap
3 1 7 2 6 8 10 4 5 9 swap 10 and 4
3 1 7 2 6 8 4 10 5 9 swap 10 and 5
3 1 7 2 6 8 4 5 10 9 swap 10 and 9
3 1 7 2 6 8 4 5 9 10