What is an Algorithm
Definition
An algorithm is a finite set of instructions that, if followed, accomplishes a particular task.
In addition, Every algorithms must satisfy the following five characteristic features
- Input: Zero or more quantities are externally supplied.
- Output: At least one quantity is produced.
- Definiteness: Each instruction is clear and unambiguous.
- Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps.
- Effectiveness: Every instruction must very basic so that it can be carried out, in principle using only pencil & paper. i.e tracing each step should be possible
Study of algorithm includes 4 distince areas one can identify:
- How to devise or design an algorithm: Creating of an algorithm is an art which may never be fully automated. It includes, writing algorithms using the existing design techniques.
- How to validate an algorithm: After the algorithm is written it is necessary to check the correctness of the algorithm. It is necessary to show that algorithm computes correct output for all possible legal inputes.
- How to analysis an algorithm: Analysis of algorithms refers to the task of determining how much computing time and storage an algorithm requires. This helps us to judge which algorithm is better.
- How to test a program: Testing consists of two phases: Debugging and Profiling
- Debugging: It is detection and correction of errors (check whether program produces faulty results for valid inputs, if it is found then the program has to be corrected).
- Profiling: It is a process of measuring the actual amount of time and space required by a corrected program to compute for valid set of inputes.
Algorithm specifications
An algorithm may be specified in any one of the following methods.
a) Natural language: Natural languages are the languages that people speak, such as English
b) Flow chart: By using a predefined geometric shapes we can describe the procedure in algorithm. Flow chart is unable to represent complex algorithms.
c) Pseudo code: It is a mixture of natural language and programming language. There are different forms of pseudo code and all these dialects are close to each other.
How to Wrtire an Algorithm (Pseudocode)
- Comments begin with // and continue until the end of line.
- Blocks are indicated with matching braces { and }.
- An identifier begins with a letter. The data types of variables are not explicitly declared.
Compound data types can be formed with records. Here is an example,
Example:Node = Record
{
datatype_1 data_1;
. .
. .
datatype_n data_n;
node *link;
}
Here link is a pointer to the record type node. Individual data items of a record can be accessed with → and period.
- Assignment of values to variables is done using the assignment statement. <Variable> := <expression>;
- There are two Boolean values TRUE and FALSE.
Logical Operators AND, OR, NOT
Relational Operators <, ≤, >, ≥, =, ≠
- A conditional statement has the following forms.
- if statement:
syntax:if <condition> then
<statement>
- if-else statement:
syntax:if <condition> then
<statement_1>
else
<statement_2>
- case statement:
syntax:Case{
:<condition_1>: <statement_1>
.
.
:<condition_n>: <statement_n>
:else: <statement_n+1>
}
- The following looping statements: while, repeat-until and For.
- While Loop:
syntax:while (condition) do
{
statement_1
.
.
statement_n
}
- repeat-until:
syntax:repeat{
statement_1
.
.
statement_n
}until (condition)
- For Loop:
syntax:for variable: = starting_value to target_value step step_count do
statement_1
.
.
statement_n
}
- Elements of multidimensional arrays are accessed using [ and ]. example: A[ i,j ]
- Input and output are done using the instructions read & write.
- There is only one type of procedure: Algorithm.
syntax:Algorithm Algorithm_Name(<Parameter list>)
Example-1:Algorithm add(a,b){
c:= a+b
return c;
}
The following algorithm finds and returns the maximum of n given numbers
Example-2:Algorithm Max(A, n)
// A is an array of sizen.
{
Result:=A[1];
for i := 2 to n do
if A[i] > Result then
Result:=A[i];
return Result;
}
Next Topic :Space Complexity