Professional Documents
Culture Documents
Discrete Structures
Discrete Structures
Result at i
th
unwinding i
T(n) = T(n-1) + 1c 1
T(n) = T(n-2) + 2c 2
T(n) = T(n-3) + 3c 3
T(n) = T(n-4) + 4c 4
An expression for the kth unwinding:
T(n) = T(n-k) + kc
Lets decide to stop at T(0). When the list to
search is empty, youre done
0 is convenient, in this example
Let n-k = 0 => n=k
Now, substitute n in everywhere for k:
T(n) = T(n-n) + nc
T(n) = T(0) + nc
( T(0) is some constant)
INSERTION SORTING
PRESENTING TWO DIFFERENT FORMULAE FOR
INSERTION SORTING.
1. Recursive formula by recursive method.
2. Iterative formula by iterative method.
PRESENTING TWO DIFFERENT FORMULAE FOR
INSERTION SORTING.
1. Recursive formula by recursive method.
2. Iterative formula by iterative method.
Algorithms of RECURSIVE METHOD:
BASIC STEP: P(0)= Any constant.
RECURSIVE STEP : For n=0 to length (Array)-1.
For c=0 to n.
if P(n+1) > P(n-c) Remain in the same order.
else Interchange P(n) and P(n+1).
Algorithms of RECURSIVE METHOD:
BASIC STEP: P(0)= Any constant.
RECURSIVE STEP : For n=0 to length (Array)-1.
For c=0 to n.
if P(n+1) > P(n-c) Remain in the same order.
else Interchange P(n) and P(n+1).
INSERTION SORT
As P(n+1)=P(n)+c
P(n)=P(n-1)+c eq*
P(n-1)=P(n-2)+c
Putting the values in eq*
P(n)=P(n-2)+c+c
P(n)=P(n-2)+2c.
(Next)
As P(n+1)=P(n)+c
P(n)=P(n-1)+c eq*
P(n-1)=P(n-2)+c
Putting the values in eq*
P(n)=P(n-2)+c+c
P(n)=P(n-2)+2c.
(Next)
Iterative method for insertion sort.
Similarly
P(n)=P(n+3)+3c
And
P(n)=p(n+4)+4c
So we will take a look of the iterations to find the
relation with in the expressions. (NEXT)
Similarly
P(n)=P(n+3)+3c
And
P(n)=p(n+4)+4c
So we will take a look of the iterations to find the
relation with in the expressions. (NEXT)
P(n)= P(n-1)+c 1
st
iteration
P(n)=P(n-2)+2c 2
nd
iteration
P(n)=P(n-3)+3c 3
rd
iteration
P(n)=P(n-4)+4c 4th iteration
P(n)=P(n-5)+5c 5
th
iteration
(NEXT)
P(n)= P(n-1)+c 1
st
iteration
P(n)=P(n-2)+2c 2
nd
iteration
P(n)=P(n-3)+3c 3
rd
iteration
P(n)=P(n-4)+4c 4th iteration
P(n)=P(n-5)+5c 5
th
iteration
(NEXT)
Writing the expression for kth term.
P(n)= P(n-k)+kc
Now we for making the formula solvable for
P(o) we put k=n which becomes
P(n)=P(n-n)+nc
P(n)=p(0)+nc
Where p(0) is some constant.
Writing the expression for kth term.
P(n)= P(n-k)+kc
Now we for making the formula solvable for
P(o) we put k=n which becomes
P(n)=P(n-n)+nc
P(n)=p(0)+nc
Where p(0) is some constant.
ALGORITHM STEPS FOR ITRATIVE
METHOD OF INSERSION SORTING
1)First of all we have the recursive formula
P(n)=P(n-1)+c, means that there is some
constant added to the previous value to get
the next value.
2)We find the value of P(n-1) which is P(n-2)+c,
and put it in the first equation.
3)In this way, we find values of P(n-2),P(n-3)and
P(n-4) and put every one of these in the
previous expression. (Next)
1)First of all we have the recursive formula
P(n)=P(n-1)+c, means that there is some
constant added to the previous value to get
the next value.
2)We find the value of P(n-1) which is P(n-2)+c,
and put it in the first equation.
3)In this way, we find values of P(n-2),P(n-3)and
P(n-4) and put every one of these in the
previous expression. (Next)
4)For finding the general formula we make the
expression for kth term. Which becomes P(n)=
P(n-k)+kc
5)As we have often given the value of P(0). So
finding P(0) we put k=n.
P(n)= P(n-n)+nc
P(n)= P(0)+nc.
(Next)
4)For finding the general formula we make the
expression for kth term. Which becomes P(n)=
P(n-k)+kc
5)As we have often given the value of P(0). So
finding P(0) we put k=n.
P(n)= P(n-n)+nc
P(n)= P(0)+nc.
(Next)
ALGORITHM STEPS FOR ITRATIVE
METHOD OF INSERSION SORTING
Algorithm check middle, then search lower
or upper
T(n) = T(n/2) + c
where c is some constant, the cost of checking the
middle
BINARY SORTING AND SEARCHING