224k views
0 votes
Describe an algorithm for finding the smallest integer in a finite sequence of natural numbers.

2 Answers

5 votes

Answer:

min = a_1

for i:= 2 to n:

if
a_i < min then min =
a_i

return min

Explanation:

We call the algorithm "minimum" and a list of natural numbers
a_1, a_2, \cdot\cdot\cdot, a_n.

So lets first set the minimum to
a_1

min = a_1

now we want to check all the other numbers.

We can use a simple for loop, to find the minimum

min = a_1

for i:= 2 to n:

if
a_i < min then min =
a_i

return min

User HkBst
by
4.0k points
4 votes

Answer:

The sequence of natural numbers is a_1, a_2, a_3, ..., a_n.

Use min as the variable that will contain the minimum value.

Set min = a_1

In a loop, compare min to each number from a_2 to an.

If min > a_i, then let min = a_i

min = a_1

for i = 2 to n

if min > a_i, then min = a_i

User Tahir Hassan
by
4.3k points