Docsity

Recursive Algorithms: Factorial and Anagram Methods, Towers of Hanoi - Prof. Sunil K. Prab, Study notes of Computer Science

Partial preview of the text

Download Recursive Algorithms: Factorial and Anagram Methods, Towers of Hanoi - Prof. Sunil K. Prab and more Study notes Computer Science in PDF only on Docsity!

Chapter 15

Recursive Algorithms and Sorting

CS 180

Sunil Prabhakar

Department of Computer Science

Purdue University

Objectives

 After this week, you should be able to

 Write recursive algorithms.

 Decide when to use recursion.

 Perform linear and binary search algorithms on

small arrays.

 Determine whether a linear or binary search is

more effective for a given situation.

 Understand selection, bubble sort, and

(recursive) quicksort algorithms.

 Understand how these algorithms compare.

Example of Recursion

Example of Recursion

 The factorial of N is the product of the first N

positive integers:

N! = N * (N – 1) * (N – 2 ) *...^ * 2 *

Example of Recursion

 The factorial of N is the product of the first N

positive integers:

N! = N * (N – 1) * (N – 2 ) *...^ * 2 *

 This is useful for many situations, e.g.

 there are n! possible sequences of n objects

 there are n!(n-k)!/k! unique subsets of size k , from a

set of size n.

 The factorial of N can be defined recursively as

Example of Recursion

 The factorial of N is the product of the first N

positive integers:

N! = N * (N – 1) * (N – 2 ) *...^ * 2 *

 This is useful for many situations, e.g.

 there are n! possible sequences of n objects

 there are n!(n-k)!/k! unique subsets of size k , from a

set of size n.

 The factorial of N can be defined recursively as

1 if N = 1 factorial( N ) = N * factorial( N-1 ) otherwise

Recursive Method

 An recursive method is a method that contains a

statement (or statements) that makes a call to itself.

 Implementing the factorial of N recursively will result in the

following method.

public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } }

Recursive Method

 An recursive method is a method that contains a

statement (or statements) that makes a call to itself.

 Implementing the factorial of N recursively will result in the

following method.

public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } Test to stop or continue.

Recursive Method

 An recursive method is a method that contains a

statement (or statements) that makes a call to itself.

 Implementing the factorial of N recursively will result in the

following method.

public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } End case: recursion stops. Test to stop or continue. Recursive case: recursion continues.

The details …

 As with any call, a recursive call results in the

creation of temporary workspace for the called

method and copying of parameters.

 Each call to a method results in the creation of a

separate workspace with new copies of variables

(local and formal parameters).

 When a recursive call ends, flow returns to the

caller.

Example

public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } factorial( 3 );

Example

N public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } factorial( 3 );

Example

N public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } factorial( 3 );

Example

N public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } factorial( 3 );