Discrete Mathematics and Its Applications, Seventh Edition

Published by McGraw-Hill Education
ISBN 10: 0073383090
ISBN 13: 978-0-07338-309-5

Chapter 3 - Section 3.1 - Algorithms - Exercises - Page 203: 34

Answer

The sorted list is $\{1,2,3,4,5,6\}$. Please check the solution to have a look at how the algorithm would work.

Work Step by Step

The key property of the bubble sort is that at the end of iteration $i$, the last $i$ values would be in the correct position. For example, after the first iteration, the greatest value would be at the last location. During each iteration, for some value $m$ and all values of $i$ such that $i < m$, we check $a_i$ and $a_{i+1}$ and interchange if need be. For this example, the list is $\{6,2,3,1,5,4\}$. During the first iteration, comparing each pair, we get the following - $\{2,3,1,5,4, \underline{6}\}$. We compare $6$ with each value and it gets to the end of the list. During the second iteration, comparing each pair, we get the following - $\{2,1,3,4, \underline{5, 6}\}.$ First, $1$ and $3$ get interchanged followed by $4$ and $5$. Notice that even though the last four are in the correct position, we cannot establish this yet after the second iteration. The only thing we know is that $5$ and $6$ are in the valid positions. During the third iteration, comparing each pair, we get the following - $\{1, 2, 3, \underline {4, 5, 6} \}$. We only make one interchange between $1$ and $2.$ Notice that even though the array is now in the proper order, there are still two more iterations in which nothing changes. Note that exercise $37$ addresses this issue and resolves it.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.