Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

DISCRETE STRUCTURES

RECUTSIVE AND ITERATIVE DEFINITION OF


SORTING AND SEARCHING ALGORITHMS
GROUP MEMBERS
NAZWAR BASHIR NAZWAR BASHIR
SYED HASSAN ZEB SYED HASSAN ZEB
MUHAMMAD ALI MUHAMMAD ALI
SHAHZAD SHAHZAD
ALGORITHMS ALGORITHMS
INTRODUCTION INTRODUCTION
CLASSIFICATION CLASSIFICATION
LINEAR LINEAR
SEARCH SEARCH
RECURSION RECURSION
FORMULA FORMULA
ITERATIVE ITERATIVE
FORMULA FORMULA
BINARY BINARY
SEARCH SEARCH
RECURSION RECURSION
FORMULA FORMULA
ITERATIVE ITERATIVE
FORMULA FORMULA
INSERTION INSERTION
SORT SORT
RECURSION RECURSION
FORMULA FORMULA
ITERATIVE ITERATIVE
FORMULA FORMULA
OUTLINE
In mathematics, computer science, and related subjects, an In mathematics, computer science, and related subjects, an
''algorithm' algorithm' is an effective method for solving a problem is an effective method for solving a problem
expressed as a finite sequence of instructions. Algorithms are expressed as a finite sequence of instructions. Algorithms are
used for calculation, data processing and many other fields. used for calculation, data processing and many other fields.
ALGORITHMS
In computer systems an algorithm is basically an
instance of logic written in software by software
developers to be effective for the intended "target"
computer(s), in order for the software on the target
machines to do something.
COMPUTER ALGORITHMS
A recursive algorithm is one that invokes
(makes reference to) itself repeatedly until a
certain condition matches, which is a method
common to functional programming.
Recursion or iteration
Linear search or sequential search is a
method for finding a particular value in a List,
that consists of checking every one of its
elements, one at a time and in sequence, until
the desired one is found.
if all list elements are equally likely to be
searched for
INTRODUCTION TO LINEAR SEARCH
LINEAR SORTING
Look at an element (constant work, c), then search the remaining
elements
RECURSIVELY
T(n) = T( n-1 ) + c
Well unwind a few of these
T(n) = T(n-1) + c (1)
But, T(n-1) = T(n-2) + c, from above
Substituting back in:
T(n) = T(n-2) + c + c
Gathering like terms
T(n) = T(n-2) + 2c
ITERATIVE METHOD
Keep going:
T(n) = T(n-2) + 2c
T(n-2) = T(n-3) + c
T(n) = T(n-3) + c + 2c
T(n) = T(n-3) + 3c (3)
One more:
T(n) = T(n-4) + 4c (4)

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

You might also like