CPNM Lecture 3 - Algorithms and Flowcharts
CPNM Lecture 3 - Algorithms and Flowcharts
Jadavpur University
2024
Algorithms
▶ An algorithm is a sequence of computational steps that transform the
input into the output
▶ An algorithm acts as a tool for solving a well-specified computational
problem
▶ The statement of the problem specifies in general terms the
desired input/output relationship
▶ The algorithm describes a specific computational procedure for
achieving that input/output relationship
▶ Example:
▶ The problem of sorting a sequence of numbers into increasing
order is formally defined as
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation (reordering ) < a1′ , a2′ , . . . , an′ > of the
input sequence such that a1′ ≤ a2′ ≤ . . . ≤ an′
▶ There are different algorithms for solving the sorting problem
(Ex.- Bubble sort, Insertion sort, Merge Sort, Queick sort etc.)
▶ Each of the sorting algorithm can have different
implementations (possibly in different programming languages)
Properties of Algorithm
▶ Finiteness: An algorithm must always terminate after a finite
number of steps
▶ Definiteness: Each step of an algorithm must be precisely
and unambiguously defined.
▶ In case of conditionals, explicit handling of all outcomes
▶ In case of loop, explicitness about when to stop
▶ Input: An algorithm has zero or more inputs, i.e, quantities
which are given to it initially before the algorithm begins.
▶ Output: An algorithm has one or more outputs i.e, quantities
which have a specified relation to the inputs.
▶ Effectiveness: All operations to be performed must be
sufficiently basic that they can be done exactly and in a finite
amount of time.
▶ Example of Non Effectiveness: Find exact value of e using the
formula
1 1 3
e =1+ + + + ...
1! 2! 3!
Summation of infinite terms requires infinite time
Complexity of Algorithm
Input: N
Output: N!
Step 1: Fact = 1
Step 2: if N > 1
Step 3: Fact = Fact * N
Step 4: N = N - 1
Step 5: goto Step 2
Step 6: Print Fact
Expressing Algorithms