Presentation is loading. Please wait.

Presentation is loading. Please wait.

Csci1300 Introduction to Programming Recursion Dan Feng.

Similar presentations


Presentation on theme: "Csci1300 Introduction to Programming Recursion Dan Feng."— Presentation transcript:

1 Csci1300 Introduction to Programming Recursion Dan Feng

2 Topics What’s recursion When should we use recursion What’s the structure of recursion Pitfalls of recursion Recursion vs. Iteration One more example

3 What’s recursion(1) Mathematics: a kind of circular definition, like a while loop contains other while statements in it. Computer Science: a function definition contains a call to itself

4 What’s recursion(2) How do you calculate 5!(factorial 5! = 1*2*3*4*5) ? Use a loop( for, while, or do … while) If I tell the 4!, now can you calculate 5! from it? 5!= 4! * 5 What’s our problem now? 4!(right) …

5 What’s recursion(3) Did you find any interesting thing: Calculations of 5! and 4! are similar Calculation of 4! is simpler than calculation of 5! They give us some hints: Use the same function to calculate both 4! and 5! The calculation of 5! can use the result of 4!

6 What’s recursion(4) int calFactorial( int num ) { if ( num == 0 ) return 1; else if ( num < 2 ) return num; else return num * calFactorial( num – 1 ) }

7 calFactorial( 5 ) : int calFactorial( int 5 ) { if ( 5 == 0 ) return 1; else if ( 5 < 2 ) return 5; else return 5 * calFactorial( 5 – 1 ) int calFactorial( int 4 ) { if ( 4 == 0 ) return 1; else if ( 4 < 2 ) return 4; else return 4 * calFactorial( 4 – 1 ) int calFactorial( int 1 ) { if ( 1 == 0 ) return 1; else if ( 1 < 2 ) return 1; else return 1 * calFactorial( 1 – 1 ) 1 4! = 24

8 What’s recursion(5) Stack A stack is a last-in/first-out memory structure. The first item referenced or removed from a stack is always the last item entered into the stack. For example, a pile of books Memory for recursion calls is a stack

9 When should we use recursion The original problem can be divided into small problems All the small problems are similar to the original problem and they are simpler than the original problem

10 What’s the structure of recursion One or more cases in which the function accomplished its tasks without the use of any recursive calls. They are called base cases One or more cases in which the function accomplishes its tasks by using recursive calls to accomplish one or more smaller versions of the task

11 Three most important things to think about when using recursion What’s the base case(s) How to divide the original problem to sub- problems How to merge the sub-problem’s results to get the result

12 Pitfalls of recursion Infinite recursion Doesn’t set the base case correctly Stack overflow There is always some limit to the size of the stack

13 Recursion vs. Iteration The problem which can be solved by recursion can also be solved by iteration, and vice verse Sometimes the way recursion solves problems is more natural than iteration Recursion does use more resources than iteration

14 One more example Sort an integer array sortByIteration( int * ) sortByRecursion( int * )

15 sortByIteration Pseudo Code: sortByIteration( int *arr, int size ) for i = 0; i < size; i++ for j = 0; j < size – 1 – i; j++ if arr[j] > arr[j+1] swap arr[j], arr[j+1] Time: size * (size – 1) / 2 ~ O(size 2 )

16 sortByRecursion Pseudo code: sortByRecursion( int *arr, int size ) if size < 2 return arr; else a1 = sortByRecursion( arr, size/2 ) a2 = sortByRecursion (&stt[size/2], size – size/2) return a = merge( a1, a2 )

17 Time to merge two sorted array is about O(size) So the total time is O(size* log size) Is it a big deal to use sortByRecursion instead of sortByIteration Yes Suppose size = 1,000 ~ 2 10 size 2 = 1,000,000 size* log size = 10,000 One hundred times faster


Download ppt "Csci1300 Introduction to Programming Recursion Dan Feng."

Similar presentations


Ads by Google