Sunteți pe pagina 1din 57

Agenda

Introduction
The idea of algorithm
Notation
Examples of algorithms

Theory of computer science


Algorithms

Alina MOMOT

alina.momot@polsl.pl

Alina MOMOT Algorithms


Agenda
Introduction
The idea of algorithm
Notation
Examples of algorithms

Agenda
1 Introduction
The aim of the lecture
The lecture topics
Literature
Structure of the course
2 The idea of algorithm
Introduction
Denition of algorithm
3 Notation
Flowcharts
PseudoC notation conventions
4 Examples of algorithms
Determination the sum of sequence elements
Determination the minimum of sequence elements
Determination of GCD
Alina MOMOT Algorithms
Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

The aim of the lecture

Understanding the basic concepts and problems of computer science


Base for other lectures, including:
Algorithms and Data Structures
Operating Systems
Computer Architecture
Assembler Programming Languages
Fundamentals of Database Systems
Computer Network

Alina MOMOT Algorithms


Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

The lecture topics


The concept of the algorithm
serial and parallel implementation, time and memory complexity
The operating system and its tasks
synchronization and communication between processes, semaphores
Computer design elements
gates, registers, buses, memory, the arithmetic and logic unit
Machine W  design and implementation
The interrupt system, Input/Output system, DMA
Programming languages
machine code, assembly language, higher level languages, compilers
Turing machine
Formal grammars, inx notation and reverse Polish notation
Data access methods: B-tree, hashing functions
Statistical model of computer as a service station (M/M/1)
New trends in computer science
Alina MOMOT Algorithms
Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

Literature

Andrew S. Tanenbaum, Todd Austin: Structured Computer


Organization. Pearson, 2013
John L. Hennessy, David A. Patterson: Computer Architecture, A
Quantitative Approach. Morgan Kaufmann, 2012
Allen B. Downey: The Little Book of Semaphores, 2016

In Polish

Wykªady z Podstaw Informatyki profesora Stefana W¦grzyna. Skrypt


uczelniany Politechniki ‘l¡skiej nr 1812

Zbiór zada« z Podstaw Informatyki. Praca zbiorowa pod redakcj¡


Stanisªawa Kozielskiego. Skrypt uczelniany Politechniki ‘l¡skiej nr 1459

Alina MOMOT Algorithms


Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

Structure of the course

Lecture
Classes
Laboratory
Exam

Semester 1: 2/2/2/E (6)


Semester 2: 1/1/-/- (2)

Alina MOMOT Algorithms


Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

Structure of the course

Lecture
Classes
Laboratory
Exam

Semester 1: 2/2/2/E (6)


Semester 2: 1/1/-/- (2)

Alina MOMOT Algorithms


Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

Structure of the course

Lecture
Classes
Laboratory
Exam

Semester 1: 2/2/2/E (6)


Semester 2: 1/1/-/- (2)

Alina MOMOT Algorithms


Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

Conditions for passing the course


Final evaluation:
Semester 1: weighted average according to the formula

final _grade = 0.15gradeclasses + 0.15gradelab + 0.7gradeexam

where
4gradeexam_tasks + 3gradeexam_teory
gradeexam =
7
Semester 2: final _grade = gradeclasses
The possibility of obtaining nal grade without writing exam:
5.0, if grcl + grlab = 10
4.5, if grcl + grlab = 9, 5
4.0, if grcl + grlab = 9
Alina MOMOT Algorithms
Agenda The aim of the lecture
Introduction The lecture topics
The idea of algorithm Literature
Notation Structure of the course
Examples of algorithms

Conditions for passing the course


Final evaluation:
Semester 1: weighted average according to the formula

final _grade = 0.15gradeclasses + 0.15gradelab + 0.7gradeexam

where
4gradeexam_tasks + 3gradeexam_teory
gradeexam =
7
Semester 2: final _grade = gradeclasses
The possibility of obtaining nal grade without writing exam:
5.0, if grcl + grlab = 10
4.5, if grcl + grlab = 9, 5
4.0, if grcl + grlab = 9
Alina MOMOT Algorithms
Agenda
Introduction Introduction
The idea of algorithm Denition of algorithm
Notation
Examples of algorithms

The idea of algorithm

Basic concept in computer science


Specication of actions to achieve a certain goal
First algorithm: GCD (greatest common divisor) by Euclid described
in Elements (c. 300 BC)
The term derives from the 9th century mathematician
Muhammad ibn Musa al'Khwarizmi, latinized 'Algoritmi'
(He gave the rules of algebraic operations on decimal numbers)

Alina MOMOT Algorithms


Agenda
Introduction Introduction
The idea of algorithm Denition of algorithm
Notation
Examples of algorithms

Denition of algorithm

Algorithm
A set of rules that precisely denes a sequence of operations
for solving a class of problems.

Features of the algorithm:


1 generality (algorithm solves a class of problems)
2 niteness (number of steps is nite)
3 deniteness (all successive states are well-dened)
4 complexity (measure of eciency)

Alina MOMOT Algorithms


Agenda
Introduction Introduction
The idea of algorithm Denition of algorithm
Notation
Examples of algorithms

Denition of algorithm

Algorithm
A set of rules that precisely denes a sequence of operations
for solving a class of problems.

Features of the algorithm:


1 generality (algorithm solves a class of problems)
2 niteness (number of steps is nite)
3 deniteness (all successive states are well-dened)
4 complexity (measure of eciency)

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Introducion

Algorithms can be expressed in many kinds of notations, including


1 natural languages
2 mathematical functions, including recursion
e.g. let a ­ b
(
a if b = 0
GCD(a, b) =
GCD(b, a mod b) if b ­ 1

3 owcharts
4 pseudocode or programming languages (Pascal, C, Java, etc.)

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Building blocks

1 Terminal, i.e. Start, Stop


2 Input/Output, i.e. read, write
3 Process, e.g. z ← z + 1
4 Decision
5 Subprogram call, e.g. MAX(a,b)
6 On-page Connector (replace
long or confusing lines on a
owchart page)
7 O-page Connector (used when
the target is on another page)

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Declarations of variables and constants

1 type_of_data name_of_variable
2 const type_of_data name_of_constant = value

Data types:
basic: bool, char, int, oat
static one-dimensional array:
type_of_data name_of_array [number_of_elements]

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Declarations of variables and constants

1 type_of_data name_of_variable
2 const type_of_data name_of_constant = value

Data types:
basic: bool, char, int, oat
static one-dimensional array:
type_of_data name_of_array [number_of_elements]

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Basic statements

Assignment:
name_of_variable = expression;
(see also x+ = 1 or i + +)
Decision (branching ow control statement):
if (condition)
{sequence_of_statements}
else
{sequence_of_statements}
(examples of conditions: a == 1 or a! = b )

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Iteration statements
Loop DO . . . WHILE
Loop WHILE
do
while (condition)
{sequence_of_statements}
{sequence_of_statements}
while (condition)

Loop FOR
for(variable_initialization; condition; modication)
{sequence_of_statements}

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Iteration statements
Loop DO . . . WHILE
Loop WHILE
do
while (condition)
{sequence_of_statements}
{sequence_of_statements}
while (condition)

Loop FOR
for(variable_initialization; condition; modication)
{sequence_of_statements}

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Iteration statements
Loop DO . . . WHILE
Loop WHILE
do
while (condition)
{sequence_of_statements}
{sequence_of_statements}
while (condition)

Loop FOR
for(variable_initialization; condition; modication)
{sequence_of_statements}

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Break and continue statements

Within loops it is possible to use break or continue statements:

break allows to exit a loop from any point within its body, bypassing its
normal termination expression

continue forces the next iteration of the loop to take place, skipping any
code in between itself and the test condition of the loop

Alina MOMOT Algorithms


Agenda
Introduction Flowcharts
The idea of algorithm PseudoC notation conventions
Notation
Examples of algorithms

Functions
The fundamental modular units, designed to perform specic tasks.
FUNCTION
type_of_result name_of_function (parameter_list)
{
sequence_of_variable_declarations
sequence_of_statements
return expression; }

type_of_result: void or type_of_data

Return is optional statement which ends function's execution from any


point of its body.
Alina MOMOT Algorithms
Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

Formulation of the problem

Provide an algorithm for calculating the sum of the values of numbers in


a given sequence {xi } of known length N , i.e.
N
X
y= xi
i=1

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y:
The index of the current element i:
The current value of xi :

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 0
The index of the current element i: 1
The current value of xi : 4

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 1
The current value of xi : 4

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 1
The current value of xi : 4
i =1<3=N

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 2
The current value of xi : 2

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 6
The index of the current element i: 2
The current value of xi : 2

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 6
The index of the current element i: 2
The current value of xi : 2
i =2<3=N

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 6
The index of the current element i: 3
The current value of xi : 5

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 11
The index of the current element i: 3
The current value of xi : 5

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute y ← 0 and i ← 1, go to 2.
2 Substitute y ← y + xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 2.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 11
The index of the current element i: 3
The current value of xi : 5
i =3=3=N

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using owchart

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop WHILE Loop DO. . . WHILE


y = 0; y = 0;
i = 1; i = 1; Loop FOR
while (i ¬ N ) do y = 0;
{ { for (i=1; i ¬ N ; i++)
y = y + xi ; y + = xi ; y + = xi ;
i = i + 1; i+ = 1;
} } while (i ¬ N )

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop WHILE Loop DO. . . WHILE


y = 0; y = 0;
i = 1; i = 1; Loop FOR
while (i ¬ N ) do y = 0;
{ { for (i=1; i ¬ N ; i++)
y = y + xi ; y + = xi ; y + = xi ;
i = i + 1; i+ = 1;
} } while (i ¬ N )

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop WHILE Loop DO. . . WHILE


y = 0; y = 0;
i = 1; i = 1; Loop FOR
while (i ¬ N ) do y = 0;
{ { for (i=1; i ¬ N ; i++)
y = y + xi ; y + = xi ; y + = xi ;
i = i + 1; i+ = 1;
} } while (i ¬ N )

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

Formulation of the problem

Provide an algorithm for calculating the minimum of the values of


numbers in a given sequence {xi } of known length N , i.e.

y = min{xi : i ∈ {1, 2, . . . , N}}

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y:
The index of the current element i:
The current value of xi :

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: ?
The index of the current element i: 1
The current value of xi : 4

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 1
The current value of xi : 4

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 1
The current value of xi : 4
i =1<3=N
Alina MOMOT Algorithms
Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 2
The current value of xi : 2

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 4
The index of the current element i: 2
The current value of xi : 2
xi = 2 < 4 = y
Alina MOMOT Algorithms
Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 2
The index of the current element i: 2
The current value of xi : 2

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 2
The index of the current element i: 2
The current value of xi : 2
i =2<3=N
Alina MOMOT Algorithms
Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 2
The index of the current element i: 3
The current value of xi : 5

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 2
The index of the current element i: 3
The current value of xi : 5
xi = 5 > 2 = y
Alina MOMOT Algorithms
Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written in natural language

1 Substitute i ← 1, go to 2.
2 Substitute y ← xi , go to 3.
3 Check whether i = N . If TRUE, then END, else go to 4.
4 Substitute i ← i + 1, go to 5.
5 Check whether xi < y . If TRUE, go to 2, else go to 3.

The given sequence {xi }: {4, 2, 5}, the known length N = 3


The current value of y: 2
The index of the current element i: 3
The current value of xi : 5
i =3=3=N
Alina MOMOT Algorithms
Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using owchart

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop DO. . . WHILE


Loop WHILE
y = x1 ;
y = x1 ;
i = 2;
i = 2; Loop FOR
do
while (i ¬ N ) y = x1 ;
{
{ for (i=2; i ¬ N ; i++)
if (xi < y )
if (xi < y ) if (xi < y )
y = xi ;
y = xi ; y = xi ;
i+ = 1;
i = i + 1;
}
}
while (i ¬ N )

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop DO. . . WHILE


Loop WHILE
y = x1 ;
y = x1 ;
i = 2;
i = 2; Loop FOR
do
while (i ¬ N ) y = x1 ;
{
{ for (i=2; i ¬ N ; i++)
if (xi < y )
if (xi < y ) if (xi < y )
y = xi ;
y = xi ; y = xi ;
i+ = 1;
i = i + 1;
}
}
while (i ¬ N )

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop DO. . . WHILE


Loop WHILE
y = x1 ;
y = x1 ;
i = 2;
i = 2; Loop FOR
do
while (i ¬ N ) y = x1 ;
{
{ for (i=2; i ¬ N ; i++)
if (xi < y )
if (xi < y ) if (xi < y )
y = xi ;
y = xi ; y = xi ;
i+ = 1;
i = i + 1;
}
}
while (i ¬ N )

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

Formulation of the problem

Provide an algorithm for calculating the greatest common divisor of given


two positive numbers a and b, i.e.

y = GCD(a, b)

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using owchart

Alina MOMOT Algorithms


Agenda
Introduction Determination the sum of sequence elements
The idea of algorithm Determination the minimum of sequence elements
Notation Determination of GCD
Examples of algorithms

The algorithm written using pseudocode

Loop WHILE
int GCD( int a, int b)
{
while (a != b)
{
if (a > b)
a -= b;
else
b -= a;
}
return a;
}

Alina MOMOT Algorithms

S-ar putea să vă placă și