creating and implementing algorithms tallinn 2015 vladimir viies, lembit jürimägi, margit aarna...

Post on 17-Jan-2016

221 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Creating and implementing algorithms

Tallinn 2015

Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

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?

but not just any instruction, instructions with certain properties.

WHAT IS AN ALGORITHM WHEN SOLVING TASKS WITH COMPUTERS?

Instructions for solving the task.

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: 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.

Instructions that can be an algorithm: there is none.

3. Task : exchange 100 euro bank note into smaller bank notes

THE ALGORITHM PROPERTIES

• Must specify inputs and outputs which are interrelated

• It should provide a solution in finite time• Must be unequivocal

......

ALGORITHM CREATING 2

Algorithm graphical presentation tools 1

(flowchart)

ALGORITHM VISUALISATION

ALGORITHM CREATING 3

Algorithm graphical presentation tools 1

There are two types of algorithms editors:

I permit only drawing the algorithm ( eg. MS Excel, UML activity…);

II additionally generate basic program code in the selected language (eg. SFC, JSP….).

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.

SFC elements of the schemes(1)

Task: find the total count of even and odd numbers in a row of numbers.. Algorithm (for writing program

code):

1. Insert data

2. Assign initial values to the counters

3. Let’s start comparing the numbers in a loop and update the counters

4. Print out the results

Algorithm realization, the language choice

Mis on algoritm?

// Program: erinevad arvud

// Author: viies // Course: iag0581

void main ()

{

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;

}

Editor SFC generated

immediately the code in C

language based on the

description of the algorithm

SFC runs!

ALGORITHM CREATING 4

Algorithm graphical presentation tools 2

(Jackson editor)

Jackson algorithm Elements Editor

ACTIVITIES

SELECTION (branched) O in right corner

REPEAT (iteration)* in right corner

An algorithm moves from top down and from left to right

Structural algorithm

The algorithm can be created as a complete solution, or consisting of sub-tasks. In the latter case, the taskneeds to be divided.

Each task can be divided into at least three parts:

DATA

INPUT

DATA

PROCESSING

RESULTS

OUTPUT

Structural approach is well suited to Jackson editor

ALGORITHM CREATING 5

Algorithm graphical presentation tools 4

(Using the UML activity diagram)

Algorithm using the UML (ArgoUML) :

Task: find the sum of positive numbers .

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)

Maatriks1( a’la tabel,kus lahtrid nummerdatud)

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A44.

{AIJ }-igal elemendil kaks indeksit, millega on määratud tema asukoht.

Maatriks2( a’la tabel,kus lahtrid nummerdatud)

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A44.

{AIJ }-igal elemendil kaks indeksit, millega on määratud tema asukoht.Indeksite väärused võivad muutuda kas 1-st või 0-st(olenevalt keelest, kus neid kasutatakse) veergude/ridade arvuni või 0-korral ühe võrra vähem.

Maatriksi elementide eristamine, teatud üldistuste alusel, on enamlevinud loogika ülesandeid programmeerimises kasutuskõlblike algoritmide loomise õppimisel.

Maatriks 3 ( a’la tabel,kus lahtrid nummerdatud)

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A44.

{AIJ }-igal elemendil kaks indeksit, millega on määratud tema asukoht.

Ruutmaatriksil(ridade arv=veergudearv) eristatakse:*peadiagonaali ,sinna kuuluvad elemendid, kus i=j ;*kõrvaldiagonaali ,sinna kuuluvad elemendid, kus i+j=const ;On vaja osata üldistades leida need elemendid, mis asuvad diagonaalide peal ja all!

Peadiagonali kohal I<J; all J<I;

Output a matrix row based on maximum element

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!

K o k k u v õ t t e k s :

1 . A l g o r i t m i k o o s t a m i n e v õ i m a l d a be r a l d a d a ü l e s a n d e s i s u l i s e l a h e n d a m i s e p r o g r a m m i k o o d i k i r j u t a m i s e s t j a s e e g a m u u d a b l a h e n d u s e s õ l t u m a t u k s k e e l e s t .

2 . V õ i m a l d a b e r i t a s a n d i t e l r ü h m a t ö ö d .

3 . M u u d a b l a h e n d u s e j ä l g i t a v u s t , l i h t s u s t a b

a l g o r i t m i k o n t r o l l i m i s t j a t õ s t a b t ö ö k i n d l u s t .

What is an algorithm?

Why use it?

How to use it?

K o k k u v õ t t e k s :

1 . A l g o r i t m i k o o s t a m i n e v õ i m a l d a be r a l d a d a ü l e s a n d e s i s u l i s e l a h e n d a m i s e p r o g r a m m i k o o d i k i r j u t a m i s e s t j a s e e g a m u u d a b l a h e n d u s e s õ l t u m a t u k s k e e l e s t .

2 . V õ i m a l d a b e r i t a s a n d i t e l r ü h m a t ö ö d .

3 . M u u d a b l a h e n d u s e j ä l g i t a v u s t , l i h t s u s t a b

a l g o r i t m i k o n t r o l l i m i s t j a t õ s t a b t ö ö k i n d l u s t .

In conclusion:

1. Algorithm creating enables separating the contents from the program code, thereby making the solution independent from writing and language.

2. Allows for different levels of group work.

3. Changes traceability of the solution,simplifies verification and increases the

reliability.

HOMEPAGE:

http://www.tud.ttu.ee/im/Vladimir.Viies/

top related