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

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 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 $\{3,1,5,7,4\}$. During the first iteration, our mini-set is $\{3, 1\}$. The algorithm inserts $1$ at the first position giving us the set $\{1,3,5,7,4\}$. During the second iteration, our mini-set is $\{1,3,5\}$. Seeing as the new value $5$ is the largest value in the mini-set, no changes need to be made. The sorted set to that point is $\{1, 3,5,7,4\}$. During the third iteration, our mini-set is $\{1,3,5,7\}$. Seeing as the new value $7$ is the largest value in the mini-set, no changes need to be made. The set is $\{1, 3, 5,7,4\}$. During the final iteration, we insert $4$ at the third location giving us the sorted set $\{1,3,4,5,7\}$. 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.