Recursion and Recursive Algorithms - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Recursion and Recursive Algorithms

Description:

The classic example of using recursion is to find the factorial of a number ... Return Address -3. Return Address -4. Return Address -5. Return Address. Factorial(-1) ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 20
Provided by: wayner2
Category:

less

Transcript and Presenter's Notes

Title: Recursion and Recursive Algorithms


1
Recursion and Recursive Algorithms
  • Module 4CC050
  • Software Development I

2
Recursion
  • Recursion happens when a method calls itself
    either directly or indirectly
  • A recursive method has to deal with two cases
  • Base Case The case to which we can provide an
    answer
  • General Case The case that expresses the
    solution in terms of a call to itself with a
    smaller version of the problem

3
Recursion
  • The classic example of using recursion is to find
    the factorial of a number
  • The factorial is defined as the number times the
    product of all the numbers between itself and 0
  • N! N (N 1)!
  • For example
  • 6! 6 5!

4
Recursion
  • Factorials
  • Base Case
  • Factorial(0) 1 (0! is 1)
  • General Case
  • Factorial(N) N Factorial(N-1)

5
Recursion
  • static double Factorial(double number)
  • if (number 0)
  • return 1
  • else
  • return number Factorial(number - 1)

6
Questions
  • Why does the previous example use values of type
    double rather than type int (given that you can
    only find the factorial of an integer?)
  • What happens if you ask this method to find the
    factorial of a negative number?

7
Stack for 4!
4
Factorial(4)
Return Address
3
calls Factorial(3)
Return Address
2
calls Factorial(2)
Return Address
1
calls Factorial(1)
Return Address
0
calls Factorial(0)
Return Address
8
Stack for -1!
-1
Factorial(-1)
Return Address
-2
calls Factorial(-2)
Return Address
-3
calls Factorial(-3)
Return Address
-4
calls Factorial(-4)
Return Address
-5
calls Factorial(-5)
Return Address
...
9
Should Recursion Be Used Here?
  • Factorials are calculated using a recursive
    algorithm.
  • However, just because an algorithm is recursive,
    it does not mean that recursion is always the
    most efficient mechanism to use, even if the
    algorithm is recursive.
  • In this case, a simple loop will do the job much
    more efficiently. This is often the case when
    the algorithm has tail recursion.

10
A Better Use of Recursion
  • QuickSort
  • Uses recursion to sort items

11
Bubble Sort
  • Repeat until no more swaps occur
  • Starting with the first list element, compare
    successive pairs of elements, swapping whenever
    the bottom element of the pair is smaller than
    the one above it

12
Bubble Sort Example - First Pass
5
4
4
4
4
4
5
3
3
3
3
3
5
2
2
2
2
2
5
1
1
1
1
1
5
13
Bubble Sort Example - Second Pass
4
3
3
3
3
3
4
2
2
2
2
2
4
1
1
1
1
1
4
4
5
5
5
5
5
14
Bubble Sort Example - Third Pass
3
2
2
2
2
2
3
1
1
1
1
1
3
3
3
4
4
4
4
4
5
5
5
5
5
15
Bubble Sort Example - Fourth Pass
2
1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4
4
5
5
5
5
5
16
Bubble Sort Example Fifth Pass
  • A final loop through the array would happen.
  • Since no swaps occur, the array must be sorted.

17
Quicksort
  • Works on the basis that it is easier to sort a
    smaller number of items.
  • The array is partitioned such that all numbers in
    the upper partition are greater than the numbers
    in the lower partition (the partitions do not
    need to be equal sizes).
  • Then each partition is recursively sorted.

18
Quicksort
If (there is more than one item in
listfirst..listlast) Select
splitVal Split the list so that listfirst..
listsplitPoint-1 lt splitVal listsplitPoint
splitVal listsplitPoint1..listlast gt
splitVal Quicksort the left half Quicksort the
right half
19
Quicksort
Write a Comment
User Comments (0)
About PowerShow.com