Professional Documents
Culture Documents
Mod1 Ds
Mod1 Ds
PROGRAMMING METHODOLOGIES
Programming Methodologies
For solving
1) we have to define the problems
2) Design algorithm to solve that problem
Two Approaches
Top – down approaches
Bottom- up approaches
Stepwise refinement
1. In the first stage, modeling, we try to represent the problem using
an appropriate mathematical model such as a graph, tree etc.
Any code may access the contents of any data structure passed to it.
Two methods may be used for modular programming are known as top-
down and bottom-up
1. Space complexity
2. Time complexity
SPACE COMPLEXITY
Analysis of space complexity of an algorithm or program is the
amount of memory it needs to run to completion.
c:fixed space
Sp(I) variable space
Space Complexity
float sum( float a, float b)
{
return a+b;
}
Sabc(I) =0
Space Complexity
float fact( int n)
{
if (n==0)
return 1
else
return n * fact(n-1)
}
The only requirement is that the time required to execute each statement
that is counted as one step be independent of the instance characteristics
i=n-1 j=n-1+1
j=n to n-1 only the false step 1
i=n-2 j=n-2+1
j=n-1 to n-1 1 times
i=n-1 j=n to n-1 0 times
0+1+2+……n-1 sum of first n-1 number
Put n-1 in equation (n-1)(n)/2
Asymptotic analysis
Asymptotic Analysis is the big idea that handles issues in
analyzing algorithms.
The list
8,2,1,5,3,9,31,12,7,18
Searching 9
Total comparison required is 6
Searching 10
Total comparison required is 10, not found
Searching 8
Total comparison required is 1.
Best case of linear search
If the searching element Is present as the first element
B(n) = O(1)
Worst case of linear search
If the searching element Is present as the last element
W(n) = O(n)
Average case of linear search
It is defined as the total time required for all possible cases /
total number of cases
n(n+1)/2*n
Hence, we can clearly say that the algorithm for which running time is
represented by the Expression 2, will grow faster than the other one,
simply by analysing the highest power coeficient and ignoring the
other constants(20 in 20n2) and insignificant parts of the
expression(3n - 4 and 100n - 2).
1<log n <sqrt(n) <n < nlogn <n2 <n3 <…….. <2n
<3n<…….nn
Example:
F(n) =2 n+3
2n+3<= 10n or 11n or 100n c=10 c=11
` g(n)=n g(n)=n
2n+3n= 5n 2n+3<=5n for all n>=1
2n+3<= 5n2
This also true
1<log n <sqrt(n) < n < nlogn <n2 <n3 <…….. <2n <3n<…….n
If n==0
Return 1
Else
Return n x fact(n-1)
O-notation [Big oh]
Example:3n+2=Ω (n) as
3n+2≥3n for n ≥1
Θ- notation[Theta]
Θ(g(n))= {f(n) :there exist positive constants C1,C2 and no such
that 0≤ C1 g(n) ≤ f(n) ≤ C2 g(n) for all n≥ no }