Documente Academic
Documente Profesional
Documente Cultură
Introduction
The idea of algorithm
Notation
Examples of algorithms
Alina MOMOT
alina.momot@polsl.pl
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
Literature
In Polish
Lecture
Classes
Laboratory
Exam
Lecture
Classes
Laboratory
Exam
Lecture
Classes
Laboratory
Exam
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
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
Denition of algorithm
Algorithm
A set of rules that precisely denes a sequence of operations
for solving a class of problems.
Denition of algorithm
Algorithm
A set of rules that precisely denes a sequence of operations
for solving a class of problems.
Introducion
3 owcharts
4 pseudocode or programming languages (Pascal, C, Java, etc.)
Building blocks
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]
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]
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 )
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}
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}
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}
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
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; }
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
y = GCD(a, b)
Loop WHILE
int GCD( int a, int b)
{
while (a != b)
{
if (a > b)
a -= b;
else
b -= a;
}
return a;
}