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

Answer

The sorted list is $\{a, b, d ,f, k, m\}$. 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 $\{d, f, k, m, a ,b\}$. Notice that in this example, the elements would be sorted according to lexicographic order (the order in the normal English alphabet). During the first iteration, comparing each pair, we get the following - $\{d, f, k, a, b, m\}$. $m$ gets swapped twice (with $a$ and $b$) putting it at the end of the list. During the second iteration, comparing each pair, we get the following - $\{d, f, a, b, k, m\}$. $k$ gets swapped twice (with $a$ and $b$) putting it at the second to last position. During the third iteration, comparing each pair, we get the following $\{d, a, b, f, k, m\}$.$f$ gets swapped twice (with $a$ and $b$). Notice how each letter $\it{slides}$ to its position. During the fourth iteration, comparing each pair, we get the following $\{ a, b, d, f, k, m\}$. $d$ gets swapped with $a$ and $b$ without any more changes needed. In the last iteration, no changes are made to the list. This example illustrates how even though the disorder among the set was borderline minimal (two sorted groups out of order), the algorithm needed multiple iterations to sort it properly.
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.