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 204: 51

Answer

$Backwaed$ $Insertion$ $sort$

Work Step by Step

Given the list is close to the correct order. Let us assume that the list is in perfect order first. $Insertion$ $sort$ compare first the second element with the first and correctly sorts these two numbers. Then it takes the next element and correctly inserts it in the part of the string that we are already know is correctly sorted, and so on. $First$ $pass$: we compare the first two elements and thus we make 1 comparison. $Second$ $pass$: We compare the third element with the first two elements thus make $2$ comparisons. $Third$ $pass$: We compare the forth element with the first element, thus we made 3 comparisons. $kth$ $pass$: On the $k$th pass, we compare the $k+1$th element with the first $k$ terms and thus we make $1$ comparison . The insertion algorithm will step if only 1 element is remaining. If the original list contains n elements, the insertion algorithm will then stop when the n -1th pass (when the nth element is compared with the last n-1 terms). $\#$ comparisons $=1+2+3+\ldots+(n-1)=\sum_{i=1}^{n-1} i$ since $\sum_{i=1}^{n} i=\frac{n(n+1)}{2}$ $\#$ comparisons $=1+2+3+\ldots .+(n-1)=\sum_{i=1}^{n-1} i=\frac{(n-1) n}{2}=\frac{n^{2}-n}{2}$ $Backward$ $Insertion$ $sort$ $Backward$ $Insertion$ $sort$ compare first the last element with the second to last element and correctly sorts these two numbers. Then it takes the previous element and correctly inserts it in the part of the string that we are already know is correctly sorted, and so on. $First$ $pass$: we compare the first two elements and thus we make 1 comparison. $Second$ $pass$: We compare the third last element with the last element(no other comparisons are needed as the element needs to inserted at the end of the already sorted list) thus make 1 comparison. $Third$ $pass$: We compare the forth last element with the last element, thus we make 1 comparison. $kth$ $pass$: On the kth pass, we compare the $k+1$th last element with the last $k$ terms and thus we make 1 comparison. The insertion algorithm will step if only 1 element is remaining. If the original list contains n elements, the insertion algorithm will then stop when the $n -1$th pass (when the nth element is compared with the last $n-1$ terms). $\#$ comparisons $=1+1+1+\ldots+1=\sum_{i=1}^{n-1} 1=n-1$ $Comparisons $ We note that both algorithms will make the same number of passes, but the backward Insertion sort will require fewer comparisons as we only need to make 1 comparison at each pass when the list is in perfect order. If the list is then close to perfect order, then we expect the backward insertion sort to require fewer comparisons.
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.