0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Al2ed chapter16

210

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
210
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
6
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Recursion Chapter 16 S. Dandamudi
• 2. Outline
• Introduction
• Recursion
• Recursion vs. Iteration
• 3. Introduction
• A recursive procedure calls itself
• Directly, or
• Indirectly
• Some applications can be naturally expressed using recursion
• factorial(0) = 1
• factorial (n) = n * factorial(n  1) for n > 0
• From implementation viewpoint
• Very similar to any other procedure call
• Activation records are stored on the stack
• 4. Introduction (cont’d)
• 5. Recursion
• Two examples in Pentium and MIPS assembly languages
• Factorial
• Quicksort
• Example 1
• Factorial
• factorial(0) = 1
• factorial (n) = n * factorial(n  1) for n > 0
• Activation record
• Consists of the return address pushed onto the stack by the call instruction
• 6. Recursion (cont’d)
• Example 2
• Quicksort
• Sort an N-element array
• Basic algorithm
• Selects a partition element x
• Assume that the final position of x is array[ i ]
• Moves elements less than x into array[0]…array[ i  1]
• Moves elements greater than x into array[ i +1]…array[ N  1]
• Applies quicksort recursively to sort these two subarrays
• 7. Recursion vs. Iteration
• Recursion
• Concise
• Better program maintenance
• Natural choice for some problems
• Potential problems
• Inefficiency
• Call invocation and return overheads
• Duplicate computation
• Increased memory requirements
Last slide