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

Answer

Please check the solution below for a comparisons between the different algorithms and the number of comparisons needed.

Work Step by Step

The list is $\{7,4,3,8,1,5,4,2\}$. What follows is the tracing of both sorting algorithms while keeping track of how many comparisons occurs. a) Insertion sort: For some iteration $i$, we try to look at the element $a_i$ and then try to find its proper location among the elements $a_1$ to $a_{i-1}$. In the algorithm, the number of comparisons done in iteration $i$ is equal to one added to the number of terms less than $a_i$ in the set $a_1$ to $a_{i-1}$. This is because the inside loop immediately stops as we meet an element larger than $a_i$ and we need at least one comparison. Therefore, we can indirectly count the number of comparisons as follows. Iteration Number $\to a_i \to$ Number of elements less than $a_i$ giving us $\alpha$ comparisons: Iteration #1 with value $4$ and $0$ elements less than $C_1=1$ Iteration #2 with value $3$ and $0$ elements less than $C_2=1$ Iteration #3 with value $8$ and $3$ elements less than $C_3=4$ Iteration #4 with value $1$ and $0$ elements less than $C_4=1$ Iteration #5 with value $5$ and $3$ elements less than $C_5=4$ Iteration #6 with value $4$ and $2$ elements less than $C_6=3$ Iteration #7 with value $2$ and $1$ elements less than $C_7=2$ Summing all the comparisons gives us $C_{tot} = 1+1+4+1+4+3+2 = 16$ comparisons. b) Binary insertion sort: The counting for this algorithm would be easier (please refer to exercise $\#49$ for a view of the pseudocode for this algorithm). This is because the number of comparisons doesn't depend on the value but on the index. In other words, the number of comparisons can be generalized and is dependent on $i$ rather than $a_i$ for some iteration $i$. Looking closely at the pseudocode, we notice that for some iteration $i$, the number of comparisions $C_i= \left\lceil \log\{i-1\}\right\rceil + 1.$ This can be reasoned as follows: For some iteration $i$, we are looking at the elements $a_1$ to $a_{i-1}$; we are halving the list size each time so logarithmic growth and add one for the base case. While this may be hard to solve and get a closed formula for, we can easily just plug in the values and get a numeric result. In our case, $C_{tot} = \sum_{i=2}^{8} \{\left\lceil \log\{i-1\}\right\rceil + 1\} = 1+2+3+3+4+4+4 = 21$. Notice that even though the binary search is more efficient, it took more comparisons for it to sort the same list. This could also be due to the fact that our algorithm does not stop when it gets it's answer; it does the largest amount of comparisons for every iteration. No matter what the initial set of values were; our algorithm would always need $21$ comparisons for a list with $8$ values.
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.