Algo
Algo
Definition
An algorithm is a set of instructions
designed to perform a specific task.
Characteristics
Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only
one meaning.
Input − An algorithm should have 0 or more well-
defined inputs.
Output − An algorithm should have 1 or more well-
defined outputs, and should match the desired output.
Finiteness − Algorithms must terminate after a finite
number of steps.
Feasibility − Should be feasible with the available
resources.
Independent − An algorithm should have step-by-
step directions, which should be independent of any
programming code.
Complexity of algorithm
Suppose X is an algorithm and n is the size of input
data, the time and space used by the algorithm X are
the two main factors, which decide the efficiency of X.
Time Factor − Time is measured by counting the
number of key operations such as comparisons in the
sorting algorithm.
Space Factor − Space is measured by counting the
maximum memory space required by the algorithm.
The complexity of an algorithm f(n) gives the running
time and/or the storage space required by the
algorithm in terms of n as the size of input data.
Expression evaluaion
While reading the expression from left to
right, push the element in the stack if it is an
operand.
Pop the two operands from the stack, if the
element is an operator and then evaluate it.
Push back the result of the evaluation.
Repeat it till the end of the expression.
456*+
456*+