Suggested languages for you:

Americas

Europe

Q20E

Expert-verifiedFound in: Page 202

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}<min$ then $min:={x}_{i}$ .

**for** $i:=2$ to n

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

**return **min, max

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}$ .

**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}$

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}<min$ 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}$ .

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}<min$ then $min:={x}_{i}$ .

**for** $i:=2$ to n

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

**return **min, max

94% of StudySmarter users get better grades.

Sign up for free