Worksheet 4.1. Example of An Algorithm
Worksheet 4.1. Example of An Algorithm
Example of an algorithm
Worksheet 4.1
Reading text
What makes a good algorithm?
Algorithm VS Recipe
Characteristics of an algorithm
If you call something “an algorithm”, it means that these characteristics are
all true:
1. An algorithm is an unambiguous description that makes clear what to
implement. In a computational algorithm, a step such as “Select a large
number” is not clear: what is “large”? 1 million, 1 billion, or 100?
2. An algorithm expects a defined set of inputs.
3. An algorithm produces a defined set of outputs.
4. An algorithm is guaranteed to terminate and produce a result. If an
algorithm could potentially be eternal and run forever, it wouldn’t be very
useful because you might never get a result.
5. The majority of algorithms are guaranteed to produce the correct
result.
An Example Algorithm find_max()
Problem: From a list of positive numbers, return the largest number on the
list.
Inputs: A list L of positive numbers. This list must contain at least one
number.
Outputs: A number n, which will be the largest number of the list.
Algorithm:
• Set max to 0.
• For each number x in the list L, compare it to max. If x is larger, set max
to x.
• max is now set to the largest number in the list.
An implementation in Python:
Does this meet the criteria for being an algorithm?
1. Is it unambiguous? Yes. Each step of the algorithm consists of primitive
operations, and translating each step into Python code is very easy.
2. Does it have defined inputs and outputs? Yes.
3. Is it guaranteed to terminate? Yes. The list L is not infinite, so after
looking at every element of the list the algorithm will stop.
4. Does it produce the correct result? Yes.
WORKSHEET 4.2
EVALUATION
Yes No Maybe
Yes No Maybe
Yes No Maybe
4. Cuando previsualicé y predije el texto de esta clase, pude adivinar unos detalles
correctamente.
Yes No Maybe