Title: Recursion and Recursive Algorithms
1Recursion and Recursive Algorithms
- Module 4CC050
- Software Development I
2Recursion
- 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
3Recursion
- 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!
4Recursion
- Factorials
- Base Case
- Factorial(0) 1 (0! is 1)
- General Case
- Factorial(N) N Factorial(N-1)
5Recursion
- static double Factorial(double number)
-
- if (number 0)
-
- return 1
-
- else
-
- return number Factorial(number - 1)
-
6Questions
- 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?
7Stack 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
8Stack 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
...
9Should 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.
10A Better Use of Recursion
- QuickSort
- Uses recursion to sort items
11Bubble 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
12Bubble 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
13Bubble 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
14Bubble 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
15Bubble 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
16Bubble Sort Example Fifth Pass
- A final loop through the array would happen.
- Since no swaps occur, the array must be sorted.
17Quicksort
- 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.
18Quicksort
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
19Quicksort