• :00Days
• :00Hours
• :00Mins
• 00Seconds
A new era for learning is coming soon

Suggested languages for you:

Americas

Europe

Q21E

Expert-verified
Found in: Page 203

### Discrete Mathematics and its Applications

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095

# Describe an algorithm that puts the first three terms of a sequence of integers of arbitrary length in increasing order.

Algorithm that that puts the first three terms of a sequence of integers of arbitrary length in increasing order is:

procedure order first three ( ${x}_{1},{x}_{2},{x}_{3},...,{x}_{n}$ : integers with $n\le 3$ ).

If ${x}_{1}<{x}_{2}$

then interchange ${x}_{1}$ and ${x}_{2}$ i.e., ${x}_{1}<{x}_{2}$

If ${x}_{2}<{x}_{3}$

Then interchange ${x}_{2}$ and ${x}_{3}$ i.e., ${x}_{2}<{x}_{3}$

return ${x}_{1}<{x}_{2}<{x}_{3}$

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 sequence of integers ${x}_{1},{x}_{2},{x}_{3},...,{x}_{n}$ .

The algorithm called order first three and the input has finite integers ${x}_{1},{x}_{2},{x}_{3},...,{x}_{n}$ .

## Step 2: Interchange for first and second term

procedure order first three ( ${x}_{1},{x}_{2},{x}_{3},...,{x}_{n}$ : integers with $n\le 3$ ).

Use the variable to interchange variables.

First check if the integer ${x}_{1}$ and ${x}_{2}$ are in increasing order, if ${x}_{1}$ and ${x}_{2}$ are not in increasing order then interchange the variables.

If ${x}_{1}>{x}_{2}$

then interchange${x}_{1}$ and ${x}_{2}$ i.e., ${x}_{1}<{x}_{2}$

## Step 3: Interchange for second and third term

Now check if the integer ${x}_{2}$ and ${x}_{3}$ are in increasing order, if ${x}_{2}$ and ${x}_{3}$ are not in increasing order then interchange the variables.

If ${x}_{2}>{x}_{3}$

Then interchange ${x}_{2}$ and ${x}_{3}$ i.e., ${x}_{2}<{x}_{3}$

## Step 4: Combine the above steps

Combine the above steps, the algorithm is:

procedure order first three ( ${x}_{1},{x}_{2},{x}_{3},...,{x}_{n}$ : integers with $n\le 3$ ).

If ${x}_{1}>{x}_{2}$

then interchange ${x}_{1}$ and ${x}_{{2}_{}}$ i.e., ${x}_{1}>{x}_{2}$

If ${x}_{2}>{x}_{3}$

Then interchange ${x}_{2}$ and ${x}_{3}$ i.e., ${x}_{2}>{x}_{3}$

return ${x}_{1}<{x}_{2}<{x}_{3}$