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

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 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 $\{6,2,3,1,5,4\}$. During the first iteration, our mini-set is $\{6, 2\}$. The algorithm inserts $2$ at the first position giving us the set $\{2, 6, 3,1,5,4\}$. During the second iteration, our mini-set is $\{2, 6, 3\}$. The algorithm inserts $3$ at the second position giving us the set $\{2,3, 6,1,5,4\}$. During the third iteration, our mini-set is $\{2, 3, 6, 1\}$. The algorithm inserts $1$ at the first position giving us the set $\{1, 2,3, 6,5,4\}$. During the fourth iteration, our mini-set is $\{1, 2, 3, 6, 5\}$. The algorithm inserts $5$ at the fourth position giving us the set $\{1, 2, 3, 5, 6,4\}$. During the final iteration, we insert $4$ at the fourth location giving us the sorted array $\{1, 2,3,4,5,6\}$. 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.