You are on page 1of 3

Example of how to use Algorithm2e

Robert Woodward
25 de diciembre de 2017

Below we illustrate the formatting as pseudo code of some sample of simple


algorithms. The goal is not to entice you to use LATEX for formatting your
algorithms as currently the best possible formatting tool for algorithms. Please
carefully check the source files and learn how to use this style. Importantly:
Always state your input

State the output if any


Always number your lines for quick referral.
Always declare and initialize your local variables

Always use \gets for assignments


Always end with “return” even when not returning any values
Use common functions and operands such as Union, PowerSet, etc. as
often as needed, unless you are asked to define them.

Algorithm 1 will find the maximum element in a finite sequence (Slide 14 in


Class Slides).

Entrada: A finite set A = {a1 , a2 , . . . , an } of integers


Salida: The largest element in the set
max ← a1
per i ← 2 to n fai
si ai > max entonces
max ← ai
fin
fine
devolver max
Algoritmo 1: Max finds the maximum number

Algorithm 2 is a greedy change-making algorithm (Slide 19 in Class Slides).


Algorithm 3 and Algorithm 4 will find the first duplicate element in a se-
quence of integers.

1
Entrada: A set C = {c1 , c2 , . . . , cr } of denominations of coins, where
ci > c2 > . . . > cr and a positive number Pk n
Salida: A list of coins d1 , d2 , . . . , dk , such that i=1 di = n and k is
minimized
C←∅
per i ← 1 to r fai
mientras n ≥ ci hacer
C ← C ∪ {ci }
n ← n − ci
fin
fine
devolver C
Algoritmo 2: Change Makes change using the smallest number of coins

Entrada: A sequence of integers ha1 , a2 , . . . , an i


Salida: The index of first location witht he same value as in a previous
location in the sequence
location ← 0
i←2
mientras i ≤ n and location = 0 hacer
j←1
mientras j < i and location = 0 hacer
si ai = aj entonces
location ← i
en otro caso
j ←j+1
fin
fin
i←i+1
fin
devolver location
Algoritmo 3: FindDuplicate

2
Entrada: A sequence of integers ha1 , a2 , . . . , an i
Salida: The index of first location witht he same value as in a previous
location in the sequence
location ← 0
i←2
mientras i ≤ n ∧ location = 0 hacer
j←1
mientras j < i ∧ location = 0 hacer
si ai = aj entonces location ← i

en otro caso j ← j + 1

fin
i←i+1
fin
devolver location
Algoritmo 4: FindDuplicate2

You might also like