Exposure C Beta 2, Chapter 1 Introduction to Hardware - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Exposure C Beta 2, Chapter 1 Introduction to Hardware

Description:

return fibo(n-1) fibo(n-2); Computing the 9th Fibo Number. Recursion Warning ... return fibo(N-1) fibo(N-2); APCS Examination Alert ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 43
Provided by: trinity1
Category:

less

Transcript and Presenter's Notes

Title: Exposure C Beta 2, Chapter 1 Introduction to Hardware


1
Exposure Java
Chapter 32 Slides
Recursion II
2
Chapter XXXII Topics
32.1 Introduction 32.2 Pre-Recursion
Assignment 32.3 Recursion Requires an
Exit 32.4 Recursion Fundamentals 32.5 Recursive
Return Methods 32.6 Fibonacci, a Recursive
Nono 32.7 Evaluating Recursive Methods 32.8
Manipulating Parameters of Recursive
Methods 32.9 Multiple Recursive Calls and the
Tower of Hanoi 32.10 Why Recursion? 32.11 The
Recursive Bubble Sort 32.12 The Merge Sort Case
Study 32.13 Mutual Recursion 32.14 Summary
3
Recursion Definition
Recursion is the computer programming process,
whereby a method calls itself.
4
APCS Examination Alert
Recursion is an important topic with frequent
questions on both the A and the AB APCS
Examination. The A Examination requires that a
student can understand and evaluate recursive
methods. The A Examination does not require that
students generate free response recursive
solutions. The AB Examination does require that
students generate some free response recursive
solutions.
5
First Recursion Rule
All recursive methods require an exit or base
case that stops the recursive process. The
stack controls recursion, since it controls
the execution sequence of methods, and stores
local method information. Every method call
requires the completion of the called method,
even if the execution sequence is interrupted by
another recursive method call. Incomplete
recursive calls result in a LIFO
execution sequence.
6
The Fibonacci Nono
The first 14 Fibonacci numbers 1 1 2 3 5
8 13 21 34 55 89 144 233 377 public
static int fibo(int n) if (n 1 n
2) return 1 else return fibo(n-1)
fibo(n-2)
7
Computing the 9th Fibo Number
8
Recursion Warning
Avoid recursive method that make a single
program statement with multiple recursive calls,
like the Fibonacci sequence. return fibo(N-1)
fibo(N-2)
9
APCS Examination Alert
Evaluating recursive method, like the examples
that follow will be required for both the A and
AB multiple choice part of the examination. Few
students are comfortable with this type of
problem at the first introduction. Most students
achieve excellent evaluation skills with repeated
practice.
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
(No Transcript)
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
2-Disk Solution
Move Disk1 from Peg A to Peg B Move Disk2 from
Peg A to Peg C Move Disk1 from Peg B to Peg C
30
3-Disk Solution
Move Disk1 from Peg A to Peg C Move Disk2 from
Peg A to Peg B Move Disk1 from Peg C to Peg
B Move Disk3 from Peg A to Peg C Move Disk1 from
Peg B to Peg A Move Disk2 from Peg B to Peg C
Move Disk1 from Peg A to Peg C
31
Tower of Hanoi Formula
There is a pattern in the number of minimum
moves to solve a given Tower of Hanoi
problem. With n the number of disks to be moved,
the formula below computes the number or required
moves. 2n - 1
32
Tower of HanoiGeneral Solution
Move n-1 disks from Source Peg to Auxiliary
Peg Move the nth disk from Source Peg to
Destination Peg Move n-1 disks from Auxiliary
Peg to Destination Peg
33
solveHanoi Method
public static void solveHanoi(char s, char t,
char d, int n) if (n ! 0) solveHanoi(s,d,
t,n-1) System.out.println("Move Disk " n "
From Peg " s " to Peg "
d) solveHanoi(t,s,d,n-1)
solveHanoi call for four disks in the main
method solveHanoi(A,B,C,4)
34
Bubble Sort and Recursion
Please do not think that recursion is
recommended for the Bubble Sort algorithm. This
section investigates some special situations
that occur with nested loops that are converted
to recursion. The Bubble Sort is a short, easy
to understand, algorithm, that uses nested loops.
The question of nested loops and recursion might
as well be addressed with a familiar algorithm.
35
Bubble Sort and Recursion
Please do not think that recursion is
recommended for the Bubble Sort algorithm. This
section investigates some special situations
that occur with nested loops that are converted
to recursion. The Bubble Sort is a short, easy
to understand, algorithm that uses nested loops.
The question of nested loops and recursion might
as well be addressed with a familiar algorithm.
36
Incorrect Base Case Solution
public static void sortList(int list , int p,
int q) int n list.length if (p lt
n-1) if (q lt n-p) if (listq gt
listq1) int temp listq listq
listq1 listq1 temp
sortList(list,p,q1) sortList(list,p1,
0)
37
Correct Base Case Solution
public static void sortList(int list, int p,
int q) int n list.length if (p lt
n-1) if (q lt n-p) if (listq gt
listq1) int temp listq listq
listq1 listq1 temp
sortList(list,p,q1) else sortList
(list,p1,0)
38
Merge Sort, the Splitting Process
39
Merge Sort, the Merging Process
40
Merge Sort Algorithm
As long as the lists are greater than one,
continue the following four steps 1 Find the
midpoint of the current list 2 Make a recursive
call to sort the first half of the list 3 Make
a recursive call to sort the second half of the
list 4 Merge the two sorted halves of the list
41
Merge Sort Source Code
public static void mergeSort(int list, int
first, int last) if (first lt
last) int mid (first last) /
2 mergeSort(list,first,mid) mergeSort(l
ist,mid1,last) merge(list,first,mid,last)

42
Mutual Recursion
Mutual recursion is the process when two methods
call each other. With two methods, called A and
B, this means that A calls B and B calls A. At
least one of the methods must have a base case
to stop the mutual recursive calls.
Write a Comment
User Comments (0)
About PowerShow.com