INTRODUCTION TO ALGORITHMS AND DATA STRUCTURES

Definition:

- An algorithm is a Step By Step process to solve a problem, where each step indicates an intermediate task. Algorithm contains finite number of steps that leads to the solution of the problem.

Properties /Characteristics of an Algorithm

  • Input-Output:- Algorithm takes ā€˜0ā€™ or more input and produces the required output. This is the basic characteristic of an algorithm.
  • Finiteness:- An algorithm must terminate in countable number of steps.
  • Definiteness: Each step of an algorithm must be stated clearly and unambiguously.
  • Effectiveness: Each and every step in an algorithm can be converted in to programming language statement.
  • Generality: Algorithm is generalized one. It works on all set of inputs and provides the required output. In other words it is not restricted to a single input value.

Categories of Algorithm

Based on the different types of steps in an Algorithm, it can be divided into three categories, namely
  • Sequence
  • Selection
  • Iteration

Sequence

The steps described in an algorithm are performed successively one by one without skipping any step. The sequence of steps defined in an algorithm should be simple and easy to understand. Each instruction of such an algorithm is executed, because no selection procedure or conditional branching exists in a sequence algorithm. Example:
// adding two numbers
Step 1: start
Step 2: read a,b
Step 3: Sum=a+b
Step 4: write Sum
Step 5: stop

Selection

The sequence type of algorithms are not sufficient to solve the problems, which involves decision and conditions. In order to solve the problem which involve decision making or option selection, we go for Selection type of algorithm. The general format of Selection type of statement is as shown below:
if(condition)
Statement-1;
else
Statement-2;
The above syntax specifies that if the condition is true, statement-1 will be executed otherwise statement-2 will be executed. In case the operation is unsuccessful. Then sequence of algorithm should be changed/ corrected in such a way that the system will re- execute until the operation is successful.

Iteration

Iteration type algorithms are used in solving the problems which involves repetition of statement. In this type of algorithms, a particular number of statements are repeated ā€˜nā€™ no. of times.
Example1:
Step 1 : start
Step 2 : read n
Step 3 : repeat step 4 until n>0
Step 4 : (a) r=n mod 10
(b) s=s+r
(c) n=n/10
Step 5 : write s
Step 6 : stop