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

Answer

procedure selectionsort $( a_1, a_2, a_3, ... a_n:$ A list of integers to be sorted$)$ for $i:=1$ to $n-1$ $\space\space\space$min$:=i$ $\space\space\space$for $j:=i+1$ to $n$ $\space\space\space$$\space\space\space$ if $a_j

Work Step by Step

This algorithm attempts to sort a list using the selection sort. It does this by looping $n-1$ times over the list and each time, selecting the smallest value and placing it at the appropriate position. After locating the smallest value, it gets swapped with whatever is in it's appropriate position. Notice how we simplified the logic by defining a swap function rather than making the sorting algorithm look bulky.
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.