Documente Academic
Documente Profesional
Documente Cultură
5.1 Introduction to Recursion 5.1.1 What is recursion? 5.1.2 Characteristics of the problems that can be solved recursively Application with Recursive & Iterative technique 5.2.1 Factorial l l ti 5 2 1 F t i l calculation 5.2.2 Multiplication series 5.2.3 Fibonacci Series 5.2.4 Quicksort Recursion with Stack
Intersession May 2009 UiTMT (MP, EZ, NIK) 1
5.2
5.3 53
Introduction to Recursion
What is Recursion?
Recursion is a programming approach or technique in to solve complex programming problems. Recursion is defined as a function/method that calls itself directly or indirectly thru another function/method that give the results in recursive calls. Usually, U ll recursive technique i used t solve a complex i h i is d to l l programmable problem such as series in mathematics and sorting problem gp Problem to be solved must have a basic method that will stop the recursive calls so that it will not get into infinite calls of functions/methods functions/methods.
Intersession May 2009 UiTMT (MP, EZ, NIK) 2
Recursion application
Examples of the mathematical problems that can be solved using recursive technique: g q 1. Factorial Calculation 2. Summation 3. 3 Multiplication 4. Fibonacci series F t i l C l l ti Factorial Calculation Popular mathematical calculation using recursive solution g g Factorial for non-negative integer n is written as n! that are composed of the product of the series as : n! = n * (n-1) * (n-2) * (n-3) * . * 1
Intersession May 2009 UiTMT (MP, EZ, NIK) 5
Base case
if n = 0 or n = 1 if n > 1
To solve recursively:
public static long factorial (int n) { if (n ==1 || n == 0 ) ( return 1; long result = n * factorial (n - 1) ; return result; } // end factorial
Intersession May 2009 UiTMT (MP, EZ, NIK)
recursive call i ll
7
base case
To solve recursively:
public static long multiply ( int a, int b ) { if (a ==0 || b ==0) return 0; else if (b==1) return a; else return a + multply(a b-1); multply(a,b-1); } // end multiply
Intersession May 2009 UiTMT (MP, EZ, NIK) 9
if b = 0 if b = 1 if b > 1
base case
5+ 5+ 5+ 5+ 5+ 5+
sum (5 - 1) = 5 + mult (4) (4 + sum (4 - 1)) = 5 + 4 + sum (3) 4 + (3 + sum (3 -1)) = 5 + 4 + 3 + sum(2) 4 + 3 + (2 + sum (2 1)) 4 + 3 + 2 + sum(1) 4 + 3 + 2 + (1) = 15
10
To l T solve recursively: l
public static long sum ( int n ) { if (n == 0 || n == 1) return n; else if ( n > 1 ) return n + sum ( n 1 ); } // end multiply
Intersession May 2009 UiTMT (MP, EZ, NIK) 11
Series
0, 1,
1,
2,
3, 5 , 8,
13,
21, ., n
Fibonacci series begin with 0 and 1 and has the property that q f p each subsequent Fibonacci number is the sum of the previous two fibonacci number. Fibonacci series at location 6 is 8
12
fibo (2) = fibo (2 1) + fibo (2 2) = fibo (1) + fibo (0) = 1 + 0 = 1 fibo (1) = 1 fibo (0) = 0
Intersession May 2009 UiTMT (MP, EZ, NIK) 13
15
16
Data Sequences : a. Best case : data almost sorted b. Medium case : data is random c. Worst case : data is in reversed sequence
17
18
> 1 E 20 30
15 E 15 S
20
30
20
30
20
30
> 0 E > 0 E 10 10
<= 15 S <= 15 E 20 S 20
> 30 E > 30
> 2 E 10
<= 15 20
> 30
Swap S ap
Intersession May 2009 UiTMT (MP, EZ, NIK) 20
<= 1 0 E
> 2 S 10 15 20 30
21
Recursive tree can be designed from the pivot value and the partition list. The level of recursive tree can show the level of difficulty in solving the recursive problem. It depends on the choice of pivot element and the sequence of data (best case, average case, worst case) f d t (b t t )
Intersession May 2009 UiTMT (MP, EZ, NIK) 22
23
n 5 4 3 2 1 0