pst1 1st Sem Bca
pst1 1st Sem Bca
Chapter 1
Introduction to Algorithms
1. Define an Algorithm.
An algorithm is a step by step procedure for performing some task in a
finite amount of time.
(OR)
An algorithm is a set of well defined instructions to solve a particular
problem. It takes a set of input and produces a desired output.
2. Mention the characteristics of Algorithm.
The important characteristics of Algorithm are
Finiteness: - An algorithm must always terminate after a finite number
of steps.
Definiteness: - each step or operation of an algorithm must be simple,
precise and unambiguous.
Input: - an algorithm has 0 or more input values.
Output: An algorithm produces at least one or more outputs.
Effectiveness: each operation or instruction should be able to carry out
in a finite amount of time.
Language independent: an algorithm must be language independent so
that the instructions in an algorithm can be implemented in any of the
languages.
3. What is an Instance of a problem? Give an example.
An instance of a problem consists of the input needed to compute a
solution to the problem. Ex: Input values like {30, 50, 40, 20, 10} in a
sorting algorithm.
4. How to decide which algorithm is best suited?
It depends on how efficient the algorithm is when higher values of
input are given.
The possible restrictions/constraints on the input values.
The architecture of the computer and the kind of storage devices to
be used.
The correctness of the algorithm
lOMoARcPSD|29302175
The order of growth ignores the constant factor needed for fixed
operations and focuses instead on the operations that increase
proportional to input size.
O (1) < O (log n) < O (n) < O (n log n) < O (n2) < O (n3) < O (2n) < O (n!)
Disadvantages:
Writing algorithm takes
a long time
Difficult to show
branching and looping in
algorithms
Understanding and writing complex logic through algorithms can
be very difficult.
Big-O
notation
Omega
notation
Theta
notation
Omega Notation ( Ω-
notation)
lOMoARcPSD|29302175
Theta notation encloses the function from above and below. Since it
represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average-case complexity of an
algorithm.
lOMoARcPSD|29302175
Step 1: start
Step 2: Declare three variables a, b and t
Step 3: Input first number and second number ‘a’ and ‘b’
Step 4: set t = a
Step 5: set a=b
Step 6: set b=t
Step 7: print a and b
Step 8: stop
lOMoARcPSD|29302175
b) Decrement n value by 1
Step 6: print the value of fact
Step 7: stop
32. Write an algorithm to generate first ’n’ terms of Fibonacci
sequence.
Step 1: start
Step 2: Declare the variables a,b, c, n, i
Step 3: Initialize variable a = 0, b = 1and i = 3
Step 4: read a number n
Step 5: print a and b
Step 6: while i is lesser than or equal to n, do the following
steps.
a) c = a + b
b) print c
c) a = b
d) b = c
e) increment i by 1 ( i = i +1)
Step 7: stop
lOMoARcPSD|29302175
a) lastdigit = number % 10
b) reverse = (reverse * 10) + lastdigit
c) number = number/10
Step 6: print reverse
Step 7: stop
34. Write an algorithm to convert character representation of a
number to its equivalent decimal format.
Step 1: start
Step 2: Declare the variables n, i and value
Step 3: Initialize value to 0 i.e. set value = 0
Step 4: read character representation of an integer number n.
Step 5: loop over the characters in n from left to right ,do the following
Steps
For i=1 to n
Value = value *
10 + (ASCII
value of i th
character –
ASCII value
of
‘0’)
Step 6: print value
Step 7: stop