Creating and implementing algorithms Vladimir Viies Lembit Jrimgi
Creating and implementing algorithms Vladimir Viies, Lembit Jürimägi, Marina Brik, Sergei Kostin, Jaan Toodre vladimir. viies@gmail. com Tallinn 2017
Knowledge and skills logical thinking sub-tasks algorithm writing the code
POINTS practice homework tests bonus
ALGORITHM CREATING 1 What is an algorithm? Why use it? How to use it?
WHAT IS AN ALGORITHM WHEN SOLVING TASKS WITH COMPUTERS? Instructions for solving the task. but not just any instruction, instructions with certain properties.
Examples: 1. Task: Find the largest number from {N} numbers Instructions that aren’t an algorithm: Compare the numbers and choose the largest number Instructions that can be an algorithm : 1. Read the numbers (remember, write down etc. ) – enter numbers. 2. Take the first number and mark it. 3. Compare marked number with next number(s) until a larger number is found or it has been compared with every number. 4. If during the comparison a larger number is found then mark it and repeat step 3.
Examples 2. Task: win 1 million in a lottery game Instructions that aren’t an algorithm: buy a ticket until you win. Instructions that can be an algorithm: there is none. 3. Task : exchange 100 euro bank note into smaller bank notes Instructions that can be an algorithm: We have to specify more details. We need to explain, what “into smaller” means, which banknotes and how many we can use. If the task is vague then we need to put restrictions into place ourselves and consider these restrictions when creating an algorithm.
ALGORITHM CREATING 2 THE ALGORITHM PROPERTIES • Must specify inputs and outputs which are interrelated • It should provide a solution in finite time • Must be unequivocal. . .
ALGORITHM CREATING 3 ALGORITHM VISUALISATION Algorithm graphical presentation tools 1 (flowchart)
SFC elements of the schemes(1) Algorithm with such a layout always has one start and one end All activities will take place in sequence! Each kind of activity has its own notation. This diagram shows the I / O, conditional and operation activities.
Algorithm realization, the language choice Mis on algoritm?
// Author: viies // Course: iag 0581 void main () { Editor SFC generated immediately the code in C language based on the description of the algorithm mitu arvu sisestan; loen mitu arvu - N; paaris ja paaritu=0; for (i = 1; i <= N; i = i + 1) { loen arvu a(i); if (kas a(i) on paarisarv(jagub 2 -ga)) { paaris=paaris+1; } else paaritu=paaritu+1; } väljasta paaris, paaritu; SFC runs! }
ALGORITHM CREATING 4 Algorithm graphical presentation tools 2 (Jackson editor)
An algorithm moves from top down and from left to right
DATA RESULTS INPUT PROCESSING OUTPUT Structural approach is well suited to Jackson editor
ALGORITHM CREATING 5 Algorithm graphical presentation tools 4 (Using the UML activity diagram)
ALGORITHM CREATING 6 Finding extremes, and "bubblesort" method
Extremes and sorting One frequent task when processing data is sorting and finding the maximum and minimum values. Such tasks always include two activities: Comparing the two values “Exchanging places” of two values The first includes one activity, but the second three activities.
A simple sorting algorithm, can also be used successfully to find extremes < MAXIMUM Analogously can be found MINIMUM
ALGORITHM CREATING 7 Shell sort algorithm
Shell sort algorithm (1) (has less exchanges, presumes that data is partially sorted)
Output matrix row, on the basis of max element Matrix input A(i, j) i, j=1. . N Max A(i, J) finding, max(i) Row separation on the base of max(i) We separate the task into sub-tasks and solve them later!
- Slides: 28