to know and use the bubble sort and shuttle sort algorithms

28
Trace an Algorithm

Upload: samson-oconnor

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: To know and use the Bubble Sort and Shuttle Sort Algorithms

Trace an Algorithm

Page 2: To know and use the Bubble Sort and Shuttle Sort Algorithms

Sorting Algorithms Lesson 1

To know and use the Bubble Sort and Shuttle Sort

Algorithms

Page 3: To know and use the Bubble Sort and Shuttle Sort Algorithms

Lesson Outcomes

Name: Know what is meant by the Bubble Sort and Shuttle Sort Algorithms

Describe: How to apply these algorithms to sort a set of numbers.

Explain: The key differences between these algorithms.

Page 4: To know and use the Bubble Sort and Shuttle Sort Algorithms

Maths

P128 Ex 8A 1,2,5,7 Next Lesson: Shell Sort and Quicksort Ada (Lovelace) King 1815 – 1852:

English Mathematician credited with the creation of the first computer program She is buried at the Church of Mary Magdalene in Hucknall.

Page 5: To know and use the Bubble Sort and Shuttle Sort Algorithms

Knowing Algorithms

As well as being able to read and trace algorithms from flow charts and pseudo code there are algorithms you are expected to know and apply from memory.

One of the biggest challenges in Decision Maths is having the time to interpret a question and apply the required algorithm... In other words you need to know each algorithm well and identify each by name.

Page 6: To know and use the Bubble Sort and Shuttle Sort Algorithms

Sorting Algorithms

Some of the most simple (but useful) Algorithms are to order a list of numbers or (letters/words) in size (or alphabetical) order.

There are four sorting algorithms you need to be able to apply and we will look at the first two today.

As people working on lists of numbers it feels unnecessary to use such methods but they are intended for use by computers on much larger lists.

Page 7: To know and use the Bubble Sort and Shuttle Sort Algorithms

The Bubble Sort

The Bubble Sort can be remembered as it puts the pairs of alternate numbers in bubbles to compare their sizes.

You can recall that it is the bubble sort by thinking that it is the algorithm where a bubble passes along the list of numbers.

Page 8: To know and use the Bubble Sort and Shuttle Sort Algorithms

9 6 2 12 11 9 3 76 9 2 12 11 9 3 76 2 9 12 11 9 3 76 2 9 11 12 9 3 76 2 9 11 9 12 3 76 2 9 11 9 3 12 76 2 9 11 9 3 7 12

The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 9 so they are exchangedThe 12 is greater than the 9 so they are exchangedThe 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged.In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged.

Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged.

The bubble sort compares the numbers in pairs from left to right exchanging when necessary.

The bubble sort compares the numbers in pairs from left to right exchanging when necessary.

The end of the list has been reached so this is the end of the first pass. The 12 at the end of the list must be the largest number in the list and so is now in the correct position. We now start a new pass from left to right.

The end of the list has been reached so this is the end of the first pass. The 12 at the end of the list must be the largest number in the list and so is now in the correct position. We now start a new pass from left to right.

Bubble Sort Example

Comparisons: Swaps:1 1

Here the first number is compared to the second and as it is larger they are exchanged.

Here the first number is compared to the second and as it is larger they are exchanged.

234567 23456

Page 9: To know and use the Bubble Sort and Shuttle Sort Algorithms

The end of the list has been reached so this is the end of the first pass. The 12 at the end of the list must be the largest number in the list and so is now in the correct position. We now start a new pass from left to right.

The end of the list has been reached so this is the end of the first pass. The 12 at the end of the list must be the largest number in the list and so is now in the correct position. We now start a new pass from left to right.

6 2 9 11 9 3 7 12Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

First Pass

Bubble Sort Example

6 2 9 11 9 3 7 12

2 6 9 11 9 3 7 122 6 9 9 11 3 7 122 6 9 9 3 11 7 122 6 9 9 3 7 11 12Second Pass

Comparisons: Swaps:7 68910111213 78910

Page 10: To know and use the Bubble Sort and Shuttle Sort Algorithms

The 11 and 12 are in the correct positions. The next pass therefore only requires 5 comparisons.

The 11 and 12 are in the correct positions. The next pass therefore only requires 5 comparisons.

Bubble Sort ExampleFirst Pass 6 2 9 11 9 3 7 12

2 6 9 9 3 7 11 12

2 6 9 9 3 7 11 122 6 9 3 9 7 11 122 6 9 3 7 9 11 12

Second Pass

Third Pass

Comparisons: Swaps:13 1014 1115161718 12

Page 11: To know and use the Bubble Sort and Shuttle Sort Algorithms

Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed.

Bubble Sort ExampleFirst Pass 6 2 9 11 9 3 7 12

2 6 9 9 3 7 11 12Second Pass

2 6 9 3 7 9 11 12Third Pass

2 6 9 3 7 9 11 122 6 3 9 7 9 11 122 6 3 7 9 9 11 12Fourth Pass

Comparisons: Swaps:18 1219202122 1314

Page 12: To know and use the Bubble Sort and Shuttle Sort Algorithms

The list is now sorted but the algorithm does not “know” this until it completes a pass with no exchanges.

The list is now sorted but the algorithm does not “know” this until it completes a pass with no exchanges.

Fifth Pass

Bubble Sort ExampleFirst Pass 6 2 9 11 9 3 7 12

2 6 9 9 3 7 11 12Second Pass

2 6 9 3 7 9 11 12Third Pass

2 6 3 7 9 9 11 12

2 6 3 7 9 9 11 12

Fourth Pass

2 3 6 7 9 9 11 12

Comparisons: Swaps:22 14232425 15

Page 13: To know and use the Bubble Sort and Shuttle Sort Algorithms

Sixth Pass

Bubble Sort Example

6 2 9 11 9 3 7 12

2 6 9 9 3 7 11 12

2 6 9 3 7 9 11 12

2 6 3 7 9 9 11 12

2 3 6 7 9 9 11 12Fifth Pass

Fourth Pass

Third Pass

Second Pass

First Pass

2 3 6 7 9 9 11 12

In this pass no exchanges have been made so the algorithm “knows” that the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

In this pass no exchanges have been made so the algorithm “knows” that the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

Comparisons: Swaps:25 152627

Page 14: To know and use the Bubble Sort and Shuttle Sort Algorithms

Quiz Time1. Which number is definitely in its correct position at the end of

the first pass?

Answer: The last number must be the largest.

Answer: Each pass requires one fewer comparison than the last.

Answer: When a pass with no exchanges occurs.

2. How does the number of comparisons required change as the pass number increases?

3. How does the algorithm “know” when the list is sorted?

4. What is the maximum number of comparisons required for a list of 10 numbers?

Answer: 9 comparisons in the first pass, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45

Bubble Sort Example

Page 15: To know and use the Bubble Sort and Shuttle Sort Algorithms

Classic Bubble Sort Questions

When completing the Bubble Sort you will be expected to..

Complete a bubble sort showing the order after each pass for larger

Answer questions about the maximum number of passes (Number of Items -1), actual number of passes, comparisons and swaps.

Page 16: To know and use the Bubble Sort and Shuttle Sort Algorithms

Quick Check

1,3,6,5,2,4 Write down the order of these

numbers after the first pass of the bubble sort.

1,3,5,2,4,6 How many comparisons and swaps in

this first pass? 5 Comparisons and 3 Swaps

Page 17: To know and use the Bubble Sort and Shuttle Sort Algorithms

The Shuttle Sort

The Shuttle Sort is another sorting Algorithm

It is advantageous over the Bubble Sort as it compares increasing numbers of values at a time meaning fewer comparisons are needed.

Unfortunately there are bubbles here too... You need to recall that the shuttle sort is the method that goes a little further each time (like running shuttles).

Page 18: To know and use the Bubble Sort and Shuttle Sort Algorithms

6 129 2 11 9 3 7

In the first pass, the first two numbers are compared. In the first pass, the first two numbers are compared.

The shuttle sort compares the numbers in pairs from left to right exchanging when necessary. An additional number is involved in each pass.

The shuttle sort compares the numbers in pairs from left to right exchanging when necessary. An additional number is involved in each pass.

The 9 is the larger and so the 9 and the 6 are exchanged.The 9 is the larger and so the 9 and the 6 are exchanged.

1st pass

Shuttle Sort Example

96

Comparisons: Swaps:1 1

Page 19: To know and use the Bubble Sort and Shuttle Sort Algorithms

In the second pass, the first three numbers are involved. The second and third numbers are compared, and swapped if necessary.

In the second pass, the first three numbers are involved. The second and third numbers are compared, and swapped if necessary.

Then the first and second numbers are compared, and swapped if necessary.

Then the first and second numbers are compared, and swapped if necessary.

Shuttle Sort Example

9 126 2 11 9 3 71st pass

Comparisons: Swaps:1 1

9 126 2 11 9 3 72nd pass 2 962

23 23

Page 20: To know and use the Bubble Sort and Shuttle Sort Algorithms

In the third pass, the third and fourth numbers are compared, and swapped if necessary.

In the third pass, the third and fourth numbers are compared, and swapped if necessary.

No swap is needed in this case, so the third pass ends here.No swap is needed in this case, so the third pass ends here.

3rd pass

Shuttle Sort Example

9 126 2 11 9 3 71st pass

6 122 9 11 9 3 72nd pass

6 122 9 11 9 3 7

Comparisons: Swaps:3 34

Page 21: To know and use the Bubble Sort and Shuttle Sort Algorithms

The passes continue in the same way.The passes continue in the same way.

The fourth pass ends here as no swap is needed.The fourth pass ends here as no swap is needed.

Shuttle Sort Example

3rd pass

9 126 2 11 9 3 71st pass

6 122 9 11 9 3 72nd pass

6 122 9 11 9 3 74th pass 6 122 9 11 9 3 7

Comparisons: Swaps: 34

1211

56 4

Page 22: To know and use the Bubble Sort and Shuttle Sort Algorithms

The fifth pass ends here as no swap is needed.The fifth pass ends here as no swap is needed.

Shuttle Sort Example

3rd pass

9 126 2 11 9 3 71st pass

6 122 9 11 9 3 72nd pass

6 122 9 11 9 3 74th pass 6 112 9 12 9 3 7

Comparisons: Swaps:6 4

5th pass 6 112 9 12 9 3 7129119

789 56

Page 23: To know and use the Bubble Sort and Shuttle Sort Algorithms

Shuttle Sort Example

3rd pass

9 126 2 11 9 3 71st pass

6 122 9 11 9 3 72nd pass

6 122 9 11 9 3 74th pass 6 112 9 12 9 3 75th pass 6 92 9 11 12 3 76th pass 6 92 9 11 12 3 7

Comparisons: Swaps:9 6

123113939363

101112131415 7891011

Page 24: To know and use the Bubble Sort and Shuttle Sort Algorithms

The list is now sorted.The list is now sorted.

Shuttle Sort Example

3rd pass

9 126 2 11 9 3 71st pass

6 122 9 11 9 3 72nd pass

6 122 9 11 9 3 74th pass 6 112 9 12 9 3 75th pass 6 92 9 11 12 3 76th pass 3 92 6 9 11 12 77th pass 3 92 6 9 11 12 7

Comparisons: Swaps:15 11

1271179797

16171819 1213141520

Page 25: To know and use the Bubble Sort and Shuttle Sort Algorithms

Classic Shuttle Sort Questions for the Shuttle Sort are Likely to be

the same as those for the bubble sort. Note that the Shuttle Sort Always uses n-1

passes as it is not until the final pass that the last number is checked.

This may make it seem less efficient than the Bubble Sort that may take n-1 or fewer passes but as each pass of the Shuttle sort can be shortened since it stops at the first none swap comparison it uses fewer comparisons (in computer terms this is more efficient).

Page 26: To know and use the Bubble Sort and Shuttle Sort Algorithms

Quick Check

1,3,6,5,2,4 Write down the order of these

numbers after the first pass of the shuttle.

1,3,6,5,2,4 How many comparisons and swaps in

this first pass? 1 Comparisons and 0 Swaps

Page 27: To know and use the Bubble Sort and Shuttle Sort Algorithms

Round Up

We have now met the idea of an Algorithm...

We have seen Algorithms as sets of instructions or pseudo code and also met algorithms you are expected to recall and apply upon request.

As we progress we will meet many more algorithms my recommendation is that you get practise using them so that their application becomes natural.

Page 28: To know and use the Bubble Sort and Shuttle Sort Algorithms

Extension