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

Answer

procedure linearsort $(a_1, a_2, a_3, ... a_n:$ A list of integers required to be sorted $)$ for $i:=2$ to $n$ index:= linearsearchmodif $(a_i$ ;$ a_1, a_2, a_3,...a_{i-1})$ {This would return the index of the first element which is greater than $a_i$} if index is not equal to $i$ $\space\space\space$store$:=a_i$ $\space\space\space$for $j:=$ index $+1$ to $i$ $\space\space\space$$\space\space\space$$a_j := a_{j-1}$ $\space\space\space a_{index} := $ store procedure linearsearchmodif $(x:$ integer$, a_1, a_2, . . . , a_n:$ distinct integers$)$ $i := 1$ while ($i ≤n$ and $x \ge a_i$ ) $\space\space\space$$i := i + 1$ return $i$

Work Step by Step

This algorithm attempts to implement a sorting algorithm based on the linear search algorithm. First we modify the linear search algorithm so it returns the index of the first element greater than some $a_i$ for the $i^{th}$ iteration. Since the left hand side of the set is sorted, that would be the location to where we should shift $a_i$ in the sort algorithm. Notice that this is very similar to the insertion algorithm (although a bit bulkier). Also notice that we can use different search algorithms to get the index. This is what exercise $44$ is about.
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.