Content of the lesson:
Algorithm is a set of precise instructions or steps to solve a given type of task. The term algorithm usually appears in the programming, where it is meant as a theoretical principle to solve a problem (in opposite to a notation in a concrete programming language). It is a prescript for the computer (processor) to make calculations to solve a given problem. A kitchen recipe can also be described as a kind of an algorithm.
If we had to refine the term for our needs we can say that an algorithm is a prescript to solve a particular problem. In other words, it is a precisely defined sequence of steps which needs an input data (correct input values) and which finds a solution using a finite number of steps. If we want to be really accurate, we can define an algorithm as a "procedure realizable by the Turing machine". Nevertheless, we do not have to define an algorithm now because this is usually solved at universities; we have to know the principles.
In practice, therefore, only those algorithms are subjects of interest which are of good quality in some sense. Such algorithms fulfill various criteria, measured e.g. by the number of steps needed to run the algorithm, or simplicity and elegance of the algorithm. Problems of efficiency of algorithms, which means methods how to choose the best algorithm from more known ones dealing with a specific problem, are dealt by branches of informatics called algorithmic analysis and theory of complexity.
A part of the text about the algorithm and its features has been taken from Wikipedia (http://cs.wikipedia.org/wiki/Algoritmus).
A computer program (thereinafter referred to as a program) is from the point of view of informatics a description of operations that solve a given assignment. The notation of an algorithm in any programming language is called a program. A program is usually produced (written) by a programmer. As an example there is a simple program that lets user enter two numbers a and b and displays their sum on the screen.
An algorithm implemented by this program could be expressed in a natural language using these steps:
Program SectiDveCisla;
var a,b:integer;
begin
readln(a);
readln(b);
writeln(a+b);
end.
import java.util.Scanner;
public class InputExp {
public static void main(String[] args) {
int a,b;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
in.close();
System.out.println(a+b);
}
}
Writing a program has its own rules, but the lucidity of the source code of the program can be improved by the way we write it. The previous sample of program can be rewritten as the following sample:
import java.util.Scanner;
public class InputExp {
public static void main(String[] args) {
int a,b;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
in.close();
System.out.println(a+b);
}
}
We can see that even if we do not understand the program it is easier to follow now.
Algorithms can be written in many ways. We will show some of them on the simple algorithm for the sum of two given numbers:
A) By verbal expression (similar to a cooking recipe):
B) Using a diagram (a simple scheme, drawing, flowchart etc.) showing the procedure:
C) Using a programming language:
Program SectiDveCisla;
var a,b:integer;
begin
readln(a);
readln(b);
writeln(a+b);
end.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
scanf("%d", &a);
scanf("%d", &b);
printf("%d",a+b);
return 0;
}