Download presentation
Presentation is loading. Please wait.
Published bySamson O’Connor’ Modified over 9 years ago
2
To know and use the Bubble Sort and Shuttle Sort Algorithms
3
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.
4
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.
5
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.
6
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.
7
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.
8
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 3 so they are exchanged. The 12 is greater than the 9 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. 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 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:11 Here the first number is compared to the second and as it is larger they are exchanged. 23456723456
9
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 12 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 12 Second Pass Comparisons:Swaps:76891011121378910
10
The 11 and 12 are in the correct positions. The next pass therefore only requires 5 comparisons. Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 2 6 9 9 3 7 11 12 2 6 9 3 9 7 11 122 6 9 3 7 9 11 12 Second Pass Third Pass Comparisons:Swaps:131014111516171812
11
Each pass requires fewer comparisons. This time only 4 are needed. Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 2 6 9 9 3 7 11 12 Second Pass 2 6 9 3 7 9 11 12 Third Pass 2 6 9 3 7 9 11 122 6 3 9 7 9 11 122 6 3 7 9 9 11 12 Fourth Pass Comparisons:Swaps:1812192021221314
12
The list is now sorted but the algorithm does not “know” this until it completes a pass with no exchanges. Fifth Pass Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 2 6 9 9 3 7 11 12 Second Pass 2 6 9 3 7 9 11 12 Third Pass 2 6 3 7 9 9 11 12 Fourth Pass 2 3 6 7 9 9 11 12 Comparisons:Swaps:221423242515
13
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 12 Fifth 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. Comparisons:Swaps:25152627
14
Quiz Time 1.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
15
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.
16
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
17
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).
18
6129211937 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 9 is the larger and so the 9 and the 6 are exchanged. 1 st pass Shuttle Sort Example 96 Comparisons:Swaps:11
19
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. Shuttle Sort Example 9126211937 1 st pass Comparisons:Swaps:11 9126211937 2 nd pass 2962 2323
20
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. 3 rd pass Shuttle Sort Example 9126211937 1 st pass 6122911937 2 nd pass 6122911937 Comparisons:Swaps:334
21
The passes continue in the same way. The fourth pass ends here as no swap is needed. Shuttle Sort Example 3 rd pass 9126211937 1 st pass 6122911937 2 nd pass 6122911937 4 th pass 6122911937 Comparisons:Swaps:34 1211 564
22
The fifth pass ends here as no swap is needed. Shuttle Sort Example 3 rd pass 9126211937 1 st pass 6122911937 2 nd pass 6122911937 4 th pass 6112912937 Comparisons:Swaps:64 5 th pass 6112912937 9119 78956
23
Shuttle Sort Example 3 rd pass 9126211937 1 st pass 6122911937 2 nd pass 6122911937 4 th pass 6112912937 5 th pass 6929111237 6 th pass 6929111237 Comparisons:Swaps:96 123113939363 1011121314157891011
24
The list is now sorted. Shuttle Sort Example 3 rd pass 9126211937 1 st pass 6122911937 2 nd pass 6122911937 4 th pass 6112912937 5 th pass 6929111237 6 th pass 3926911127 7 th pass 3926911127 Comparisons:Swaps:1511 1271179797 161718191213141520
25
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).
26
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
27
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.