 Suggested languages for you:

Europe

Answers without the blur. Sign up and see all textbooks for free! Q20E

Expert-verified Found in: Page 202 ### Discrete Mathematics and its Applications

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095 # Describe an algorithm for finding both the largest and the smallest integers in a finite sequence of integers.

Algorithm that produces the minimum(smallest) and maximum(largest) integers in a finite sequence of integers ${x}_{1},{x}_{2},{x}_{3}$ is:

procedure min and max ( ${x}_{1},{x}_{2},{x}_{3}$ : integers with $n\ge 1$ ).

role="math" localid="1668421689261" $min:={x}_{1}\phantom{\rule{0ex}{0ex}}max:={x}_{1}$

for $i:=2$ to n

If ${x}_{i} then $min:={x}_{i}$ .

for $i:=2$ to n

If ${x}_{i}>max$ then $max:={x}_{i}$ .

return min, max

See the step by step solution

## Step 1: algorithm

Algorithm is a finite sequence of precise instructions that are used for performing a computation or for a sequence of steps.

First, Assume the finite set of integers ${x}_{1},{x}_{2},.....{x}_{n}$ .

The algorithm called min and max and the input has finite integers ${x}_{1},{x}_{2},.....{x}_{n}$ .

## Step 2: Minimum and Maximum

procedure min and max ( ${x}_{1},{x}_{2},.....{x}_{n}$ : integers with $n\ge 1$ ).

Initially consider the minimum and maximum as the first term in the list, if the value is not minimum or maximum then the value will be adjusted later in algorithm.

$min:={x}_{1}\phantom{\rule{0ex}{0ex}}max:={x}_{1}$

## Step 3: determine the minimum and maximum

In case the value for second term and term is smaller than the current minimum, then reassign the position of this term to the minimum.

for $i:=2$ to n

If ${x}_{i} then $min:={x}_{i}$ .

Also in case, if the value for second term and ${n}^{th}$ term is greater than the current maximum then reassigns the position of this term to the maximum.

for $i=2$ to n

If ${x}_{i}>max$ then $max:={x}_{i}$ .

## Step 4: Combine above steps

Combine the above steps then the algorithm is:

procedure min and max ( ${x}_{1},{x}_{2},.....,{x}_{n}$ : integers with $n\ge 1$ ).

$min:={x}_{1}\phantom{\rule{0ex}{0ex}}max:={x}_{1}$

for $i:=2$ to n

If ${x}_{i} then $min:={x}_{i}$ .

for $i:=2$ to n

If ${x}_{i}>max$ then $max:={x}_{i}$ .

return min, max ### Want to see more solutions like these? 