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

Answer

The sorted list is $\{1,3,4,5,7\}$. 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 $\{3, 1, 5, 7, 4\}$. During the first iteration, comparing each pair, we get the following - $\{1, 3, 5, 4, 7\}$. $1$ and $3$ get swapped and same thing with $4$ and $7$. Notice that 7 is the only element in the correct position During the second iteration, comparing each pair, we get the following - $\{1, 3, 4, 5, 7\}.$ In this iteration, $4$ and $5$ get swapped. 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.