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

Answer

The sorted list is $\{1,2,3,4,5,6\}$. Please check the solution to see how the algorithm arrives at this solution.

Work Step by Step

The binary insertion sort is characterized by finding the next index using a binary search algorithm. The sort main loop occurs $n-1$ times (from $2$ to $n$) and for each iteration $i$, the proper index of $a_i$ is found in the previous terms $a_1$ to $a_{i-1}$. In this example, the list is $\{3,2,4,5,1,6\}$ For the first iteration, the binary search is called on the mini-set $\{3\}$ with target $:=2$. The index returned is $1$ and hence $2$ is moved to that index giving us $\{2,3,4,5,1,6\}$. For the second iteration, the binary search is called on the mini-set $\{2, 3\}$ with target $:=4$. The index returned is $3$ and hence no change is done to the list. For the third iteration, the binary search is called on the mini-set $\{2, 3, 4\}$ with target $:=5$. The index returned is $4$ and hence no change is done to the list. For the fourth iteration, the binary search is called on the mini-set $\{2, 3, 4, 5\}$ with target $:=1$. The index returned is $1$ and hence $1$ $\it crawls$ to that location, shifting everything else one space to the right giving us $\{1,2,3,4,5,6\}$. For the third iteration, the binary search is called on the mini-set $\{1, 2, 3, 4, 5\}$ with target $:=6$. The index returned is $6$ and hence no change is done to the list.
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.