StudySmarter AI is coming soon!

- :00Days
- :00Hours
- :00Mins
- 00Seconds

A new era for learning is coming soonSign up for free

Suggested languages for you:

Americas

Europe

Q21E

Expert-verifiedFound in: Page 203

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

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

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

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

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

94% of StudySmarter users get better grades.

Sign up for free