Lec11RecursionInClass

# Lec11RecursionInClass - Recursion Sequential code ->...

This preview shows pages 1–2. Sign up to view the full content.

Recursion Sequential code -> iteration (loops) Loops -> recursion Factorial int fact = 1 * 2 * 3 * 4 * 5 * … * 99 for (int i = 0; i < 99; i++) fact = fact * i; Recursion – we need to be able to express the problem in terms of solving a smaller, identical problem. 99! – what smaller, identical problem would save me work if I already knew the answer? 99! = 98!*99 Smaller case – (n-1)! – the identical, smaller problem General case – which modifies the smaller problem ((n-1)!*n) Base case – has no smaller case – 1! = 1, 0! = 1 We want to find the maximum number in an array of n integers Smaller case – maximum number of an array of n-1 integers General case – max ( max of (n-1) integers, nth integer) Base case – max of an array of 1 integer is that integer We want to find the maximum number in a linked list that has n elements, and each link has an integer in it Smaller case – find the max number in a linked list with n-1 elements in it General case – max (max of (n-1) elements, nth element)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/27/2011 for the course CMPSC 24 taught by Professor Agrawal during the Fall '09 term at UCSB.

### Page1 / 2

Lec11RecursionInClass - Recursion Sequential code ->...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online