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

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 insertion sort is that at we sort the array one value at a time. We add a new value each iteration and $\it{insert}$ it at the appropriate location. For this example, the list is $\{d,f.k,m,a,b\}$. Note that the values are compared according to their lexicographic value (order in the normal English alphabet). During the first iteration, our mini-set is $\{d,f\}$. Seeing as the new value $f$ is the largest value in the mini-set, no changes need to be made. The sorted set to that point is $\{d,f.k,m,a,b\}$. During the second iteration, our mini-set is $\{d,f, k\}$. Seeing as the new value $k$ is the largest value in the mini-set, no changes need to be made. The sorted set to that point is $\{d,f.k,m,a,b\}$. During the third iteration, our mini-set is $\{d,f, k, m\}$. Seeing as the new value $m$ is the largest value in the mini-set, no changes need to be made. The sorted set to that point is $\{d,f.k,m,a,b\}$. During the fourth iteration, we insert $a$ at the first location giving us the sorted set $\{a,d,f,k,m,b\}$. During the final iteration, we insert $b$ at the second location giving us the sorted set $\{a,b,d,f.k,m\}$. Notice how in contrast to the bubble sort, no element is guaranteed to be in the correct position until the end of the last iteration. Also notice that when we say insertion, the algorithm does that by linearly swapping each pair value until we reach the target.
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.