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

Answer

procedure extremes $( a_1, a_2, a_3, ... a_n:$ finite sequence of integers$)$ smallest $:=$ $a_1$ largest $:= a_1$ for $i:=1$ to $n$ $\space\space\space$ if $a_i$ largest then largest$:= a_i$ return $\{ $ smallest, largest $\}$

Work Step by Step

The algorithm attempts to find the smallest and largest values in a sequence of integers. We initialize two parameters to store the smallest and largest values for every value we encounter as we loop through the array. We return both values as a set of values. Note that an alternate solution could sort the sequence and then return the smallest and largest values immediately (the first and last values respectively). This would be overkill and would be computationally costly (a lot more steps required to get the result.)
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.