Algorithms
Algorithms
Structures
Lesson 2: Algorithms
Objectives
• At the end of this lesson you are expected to be able to
• Explain what an algorithm is
• Outline the properties of a algorithm
• Express an algorithm using pseudocode
• Convert pseudocode to flowchart
• Follow the execution of flowchart
• Design simple algorithms
• Count number of operations in an algorithm
Outline
• Overview
• Pseudocode
• Control Structures
• Arrays
Overview
• Definition
• An algorithm is a finite set of precise instructions to solve a specific problem.
It is sometimes referred to as an effective procedure.
• Every Computer program, implements an algorithm.
• Every algorithm is meant to solve a particular problem
• It usually follows the input-process-output cycle i.e.
• Input: it is given data to work with, usually as parameters
• Process: it the works on this input
• Output: returns the results of processing, could be by displaying to the
console or saving to memory
Characteristics
• An algorithm has the following characteristics
• Precision
• Steps are precisely stated
• Uniqueness
• Intermediate steps are uniquely defined and depend on inputs
• Finiteness
• The algorithm terminates.
• Input
• Receives data to be processed
• Output
• Produces output
• Generality
• Should apply to a set of inputs
Metrics of Algorithms
• An algorithm is designed, it is evaluated against the following
• Correctness
• It should be able to produce correct results for any given set of legal inputs.
• Efficiency
• How efficient does the algorithm utilizes the computing resources?
• Resources include
• CPU time
• Execution time
• Memory
• Bandwidth
• Simplicity
• In terms of design and implementation
Example
• An algorithm for a simple adding machine could be described as
follows
• Adder
• Read two numbers
• Add them
• Display the sum
Notation for Algorithms
• Algorithms can be specified in
• Plain English (or any natural language)
• For humans not computers
• Difficult to transform into a computer program
• Computer program
• To specific
• Language and at times platform dependent
• In between these two extremes is pseudocode
• Resembles actual code but allows descriptions in English for complex procedures.
• It is precise, structured and not specific to a programming language
• Provides notation for programming control structures, like looping and alternation
Notation for Algorithms
• Constants
• 2, 2.4, “Text” etc
• Variables
• Alphabetical letters, A, B, C, …
• All arithmetic operations are accepted
• +, -, *, /, <, >, >=, <=
• Arithmetic expressions
• 2+A(B – C)
• Assignments
• Storing data to variables
• Variable := expression e.g. A := 2, B := A + 3
Notation for Algorithms
• Adder problem revisited
• Adder
• A := read from user
• B := read from user
• Z := A + B
• Display Z
• Commands
• For input we will use READ to read from user
• For output we will use
• RETURN to return the processed result or
• PRINT to display to standard output console
• PRINTLN, PRINT with carriage return
Notation for Algorithms
• Still our Adder problem
• Adder
• A := READ
• B := READ
• Z := A + B
• PRINT Z
Notation for Algorithms
• A variable holds only one element
• Most programs process collections of data, e.g. MIS, Payrolls e.t.c.,
have databases in the backend.
• We will use an array, a collection of similar objects denoted by the
name of array followed by ‘(‘ and ‘)’. E.g. X() is an array named X.
• Each slot has a unique identifier called an index with the first slot indexed 0.
• So an array of size n has indices 0 t0 n-1
• To access individual slots we use X(index), e.g. X(0) refers to the first slot in
the array named X
Control Structures
• Control structures determine the flow of control through any given algorithm
• Implemented with three basic types
• Sequential: default mode. Sequential execution of code statements (one line after
another) -- like following a recipe
• Selection: or alternation, used for decisions, branching -- choosing between 2 or more
alternative paths.
• if
• if/else
• Repetition: used for looping, i.e. repeating a piece of code multiple times in a row. In
C++, there are three types of loops:
• For
• while
Selection
• The IF <condition> THEN statement, decides, based on the condition,
whether a body of statements should be executed.
• Syntax
• IF <condition> THEN DO
• BODY
• END IF
• Example
• IF N >= 2 DO
• PRINT “N is greater than 2”
• END IF
Selection
• The IF <condition> THEN..ELSE statement, decides, based on the condition, which of the two,
usually mutually exclusive body of statements should be executed.
• Syntax
• IF <condition> THEN DO
• TBODY
• ELSE
• FBODY
• END IF
• Example
• IF N >= 2 DO
• PRINT “N is greater than 2”
• ELSE
• PRINT “N is less than 2”
• END IF
Repetition
http://elearning.algonquincollege.com/coursemat/mcintyb/
dat2219d/lectures/27-Flowcharting.htm
Repetition
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A
I :=0 N N-1 I A(I) S I<=N-1
S:=0 5 4
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 T
S:=0
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 2
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 6
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 12
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 20
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20 T
END WHILE
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20 T
END WHILE 30
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20 T
END WHILE 5 X 30
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20 T
END WHILE 5 X 30 F
RETURN S
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20 T
END WHILE 5 X 30 F
RETURN S
S=30
END.
Processing Arrays A
0 1 2 3 4 Index
2 4 6 8 10 Value
• N = 5, N-1 = 4,
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A N N-1 I A(I) S I<=N-1
I :=0 5 4 0 2 0 T
S:=0 1 4 2 T
WHILE I <= N-1 DO 2 6 6 T
S:=S+A(I) 3 8 12 T
I:= I +1 4 10 20 T
END WHILE 5 X 30 F
RETURN S S=30
END.
Class Exercise
• Write a FOR version of sumList below
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A
I :=0
S:=0
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Class Exercise
• Convert the program to avgList, so it returns the average instead of
sum
Algorithm sumList(A, N)
Input: A an array of n integers
Output: sum of all numbers in A
I :=0
S:=0
WHILE I <= N-1 DO
S:=S+A(I)
I:= I +1
END WHILE
RETURN S
END.
Class Exercise
• Write a program which takes a list of numbers and returns the largest
number in the list
Class Exercise
• Write a program that receives a positive integer n and prints numbers
1 to n
Example
• Write a program arithSum(N) which gets a positive integer N and
returns the sum of the first N integers (1 + 2 +..+ N) .
• Solution Algorithm arithSum( N)
Input: N, a positive integer
Output: returns 1 + 2 + .. + N
I :=0
S:=0
WHILE I <= N DO
S:=S+I
I:= I +1
END WHILE
RETURN S
END.
Example
• Write a program rem(N,M) which gets positive integers M and N and
returns the remainder of dividing N by M, using only addition and
subtraction operations.
• Solution
• Lets devise an algorithm
• We see that if M is greater than N then we
• return N.
• Otherwise we repeatedly subtract M from N and assigning the balance to N, until N is
less than M
Example
• Write a program rem(N,M) which gets positive integers M and N and returns the
remainder of dividing N by M, using only addition and subtraction .
• Solution
Algorithm rem(N, M)
Input: M, N, positive integers
Output: returns remainder of N/M
WHILE N > M DO
N := N - M
END WHILE
RETURN N
END.
Example
• Write a program div(N,M) which gets positive integers M and N and returns the the number of
times M is in N (the whole part of N/M), using only addition and subtraction .
• Solution
Algorithm div(N, M)
Input: M, N, positive integers
Output: returns 1 + 2 + .. + N
I := 0
WHILE N > M DO
N := N – M
I := I + 1
END WHILE
RETURN I
END.
Example
• Write a bits(N), which gets a positive integer and prints the bits in the binary representation of
N.
• Solution
Algorithm bits(N)
Input: N, a positive integer
Output: returns whole part of N/M
WHILE N > 0 DO
PRINT rem(N, 2)
N := div(N,2)
END WHILE
RETURN N
END.
Nested Loops
• Sometimes the body of a loop construct, could be a loop as well.
• E.g.
FOR I := 0 TO 10 DO
FOR J:= 0 TO 10 DO
PRINT J
END FOR
PRINTLN
END FOR
FOR I := 1 TO 10 DO
FOR J:= 1 TO 10 DO
PRINT I*J
PRINT “ “
END FOR
PRINTLN
END FOR
PRINT I*J 2 4 6 8 10
PRINT “ “ 3 6 9 12 15
END FOR 4 8 12 16 20
PRINTLN 5 10 15 20 25
END FOR
FOR I := 1 TO 10 DO
FOR J:= I +1 TO 10 DO
IF A(I) = A(J) THEN DO
RETURN TRUE
END IF
END FOR
PRINTLN
END FOR
END.
Summary
• We looked at
• algorithms, their characteristics and metrics, pseudocode and flowcharts
• Control structures
• Sequencing
• Selection
• Repetition
• Arrays
• Nested loops
• ?????QUESTIONS?????