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

Answer

All three sorted lists are $\{1,2,3,4,5\}$. Check the solution for an explanation of how the selection sort worked.

Work Step by Step

The selection sort is characterized by doing the following for each iteration $i$: It selects the smallest value and inserts it at the $i^{th}$ location by swapping it whatever value is at that location. a) We begin with the set $\{3,5,4,1,2\}$ For the first iteration, the algorithm loops over the entire set and decides that $1$ is the least element. It is swapped with the element in that location giving us $\{1,5,4,3,2\}$ For the second iteration, the algorithm loops over the entire set and decides that $2$ is the least element. It is swapped with the element in that location giving us $\{1,2,4,3,5\}$ For the third iteration, the algorithm loops over the entire set and decides that $3$ is the least element. It is swapped with the element in that location giving us $\{1,2,3,4,5\}$ For the final iteration, the smallest value is at the appropriate location and hence no changes are made. b) We begin with the set $\{5,4,3,2,1\}$ For the first iteration, the algorithm loops over the entire set and decides that $1$ is the least element. It is swapped with the element in that location giving us $\{1,4,3,2,5\}$ For the second iteration, the algorithm loops over the entire set and decides that $2$ is the least element. It is swapped with the element in that location giving us $\{1,2,3,4,5\}$ For the third and fourth iteration, the smallest value is at the appropriate location and hence no changes are made. c) b) We begin with the set $\{1,2,3,4,5\}$ For every iteration of the algorithm, the smallest value is at the appropriate location and hence no changes are made.
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.