0% found this document useful (0 votes)
56 views

Iteration and Recursion

Iteration involves repeatedly performing the same processing steps to solve a problem. Each repetition is called an iteration. Common problems solved through iteration include calculating totals and processing arrays. Recursion involves a function calling itself to break a problem down into smaller sub-problems until it reaches a base case. It uses a divide-and-conquer approach. Recursion is often a more natural way to express certain algorithms like calculating factorials or solving Towers of Hanoi. While generally less efficient, recursion may be preferable when it significantly simplifies the problem or when an iterative solution is difficult to express.

Uploaded by

jehoshua35
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Iteration and Recursion

Iteration involves repeatedly performing the same processing steps to solve a problem. Each repetition is called an iteration. Common problems solved through iteration include calculating totals and processing arrays. Recursion involves a function calling itself to break a problem down into smaller sub-problems until it reaches a base case. It uses a divide-and-conquer approach. Recursion is often a more natural way to express certain algorithms like calculating factorials or solving Towers of Hanoi. While generally less efficient, recursion may be preferable when it significantly simplifies the problem or when an iterative solution is difficult to express.

Uploaded by

jehoshua35
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ITERATION AND

RECURSION
Problem Solving: Iteration and Recursion
Many problems can be solved using either iteration or
recursion.

Iteration: Iteration is simply the repetition of processing

steps.
The number of repetitions/iterations required to solve a
problem can be determined by many different factors. E.g. To
calculate the total of your stock portfolio, you would iterate
over your stock holdings, keeping a running total until each
holding has been processed.
 In this case, the number of repetitions is determined by how
many stock holdings you happen to have.
 In this case, the number of repetitions is determined by how
many stock holdings you happen to have.
 Iteration lends itself more readily to solving some problems
while for others recursion can seem more natural.
 Iteration is a very simple, straightforward approach to
solving many common problems such as performing
calculations and processing arrays.
Iteration
• Definition
– Repetition of processing steps
– The act of repeating a process with the aim of reaching a
desired goal. Each repetition of the processing step is called
an iteration
– iteration like recursion is based on a control structure: namely
the for loop, do while , repeat until.
– Within the above control structures there is a loop repetition
test that determines when to exit from the loop. The three main
test you usually come across are
Iteration
.
**********************************************
Until i < 10

int result = 1;
***********************************************
for (int i = 0; i < exponent; ++i) {
result *= base;
}
return result;
**********************************************
while (i < 10 )
{
………
………..
...i = I + 1
}
Iteration
• Two basic examples of functions using iteration
• This function sums up and returns the result of a list of values
def sum(list) :
result = 0
for x in list
result += x
return result
– Given an array A of N integer elements return the sum of all elements in
array A .
Iteration
• This function returns the value of the base raised to the power of the exponent

package com.wrox.algorithms.iteration;
public final class PowerCalculator {
public static final PowerCalculator INSTANCE = new PowerCalculator();
private PowerCalculator() {
}
public int calculate(int base, int exponent) {
assert exponent >= 0 : “exponent can’t be < 0”; <------------ validity of exponent
int result = 1; <------------ initialize the result
for (int i = 1; i < exponent; ++i) {
result *= base; <------------ (ITERATION

}
return result;
}
}

Then comes the iteration in the form of a for loop.


If the exponent was 0, the loop would terminate without performing any multiplication and the result would be 1. If the exponent
was 1 then the result would be the value of the base.
Iteration

for (int i = 0; i < orders.length; ++i) {


orders[i].applyDiscount(percentage)
};

• Given an array of Orders this iteration goes


through each order and applies a discount
• Iterations can also be done in reverse. In
general various operations can be applied
on elements in an array
Recursion: A recursive algorithm involves a
method or function calling itself. It does this by
breaking a problem into smaller and smaller parts,
each looking very similar to the larger part yet finer
grained.
Recursion uses a divide-and-conquer approach
whereby a method makes repeated, nested calls to
itself. It is often a better choice for processing nested
data structures.
Recursion can often, though not always, be a more
natural way of expressing an algorithm than iteration.
Recursion
by example
• 7! equals 7*6*5*4*3*2*1 .
• 7! equals 7*6!.
This is now a simpler version of the original problem and we even know how
to make our problem progressively more simple.
• The problem has also been defined in terms of itself. i.e. we defined 7! in
terms of 6!. the essence of recursive problem solving.
• What is the base case is. What is the simplest factorial? 1!.
– 1! equals 1.
Recursion
• The numerous common examples used to demonstrate
this technique are to name a few :
– Calculating triangular numbers *
– Factorials
– Generate anagrams
– Binary search
– Solve Tower of Hanoi puzzle *
– Investigate mergesort sorting technique *
Recursion
by example
• Triangular numbers
• 1
1+2=3
(1+2)+3=6
(1+2+3)+4=10
(1+2+3+4)+5=15

• Finding the nth Term Using a Loop


• Suppose you wanted to find the value of some arbitrary nth term in the series
—say the fourth term (whose value is 10). How would you calculate it? You
might decide that the value of any term can be obtained by adding up all the
vertical columns of squares.
Why Use Recursion ?
• Given that recursion is in general less efficient, why would we use
it? There are two situations where recursion is the best solution:
• The problem is much more clearly solved using recursion: there
are many problems where the recursive solution is clearer,
cleaner, and much more understandable. As long as the efficiency
is not the primary concern, or if the efficiencies of the various
solutions are comparable, then you should use the recursive
solution.
Why Use Recursion ?
• Some problems are much easier to solve through
recursion:
• there are some problems which do not have an easy
iterative solution. Here you should use recursion.
• The Towers of Hanoi problem is an example of a problem
where an iterative solution would be very difficult.
Why Use Recursion ?
• Recursion is used to make code less sloppy, keep in mind
it is usually slower and requires more memory.

You might also like