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...

7
9 6 2 12 11 9 3 7 6 9 2 12 11 9 3 7 6 2 9 12 11 9 3 7 6 2 9 11 12 9 3 7 6 2 9 11 9 12 3 7 6 2 9 11 9 3 12 7 6 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 : 1 1 Here the first number is compared to the second and as it is larger they are exchanged. 2 3 4 5 6 7 2 3 4 5 6

Transcript of 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...

Page 1: 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.

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 2: 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.

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 3: 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.

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 4: 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.

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 5: 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.

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 6: 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.

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 7: 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.

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