Monday, 1 August 2011

Sorting

Use sorting to write the numbers 14, 10, 6, 15, 9, 21, 17 in ascending order.

  • Bubble Sort
Look at the first 2 numbers, if in right order leave them, if not swap them.

Next pair of numbers will be dealt with the same way as the first 2 numbers.



Continue until you reach the other end, this is called a pass.

Go back to beginning and do it again until all numbers are in order.














4 Passes   7 swaps   21 comparisons


  • Shuttle Sort
Look at the first 2 numbers, if in right order leave them, if not swap them, this is the first pass.

Next pair of numbers will be dealt with the same way as the first 2 numbers, if swap was made, compare the first number to the swapped one, swap if wrong way round, this is the second pass.

Compare third and fourth numbers, if swapped compare swapped number to the one before it and swap if needed.

Keep going backwards until no more swaps, this ends that pass.

Continue until all numbers are in the right order.



















6 Passes   7 Swaps   11 Comparisons


©2011 Grant Dwyer

No comments:

Post a Comment