76,410 views
30 votes
30 votes
Describe an algorithm for finding the smallest integer in a finite sequence of natural numbers.

User PropellerHead
by
2.7k points

2 Answers

10 votes
10 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 Nick Bondarenko
by
2.9k points
23 votes
23 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 Dsounded
by
3.5k points