Download presentation

Presentation is loading. Please wait.

Published byBryce Shroff Modified over 3 years ago

1
Recursion 2014 Spring CS32 Discussion Jungseock Joo

2
Abstract Class A class with one or more pure virtual functions: – Person = {Male | Female} – You cant directly create an object: Person person; -> error Male male; -> ok Female female; -> ok – When Male & Female classes have the implementations of print_info()

3
Inheritance Review Virtual destructors of base classes – delete pPer invokes only the destructor of Person if not virtual. – Resources of the derived part in Employee class not destroyed – Memory leak

4
Inheritance Review Order of construction of derived classes – A derived object : base part + derived part

5
Inheritance Review Order of construction of derived classes – A derived object : base part + derived part 1. Construct the base part Calls the constructor of the base class Member initialization list 2. Construct the data members of derived part Member initialization list 3. Execute the body of the constructor of the derived class

6
Inheritance Review Destruction : Backward 1. Execute the body of the destructor of the derived class 2. Destroy the data members of derived part 3. Destroy the base part

7
Recursion

8
Recursive Function Calls itself in the function body Ex. Fibonacci number

9
Recursion vs. Iteration

10
Recursion Divide-and-conquer – Solve sub-problems and merge the sub-solutions Mathematical Induction – Base case (terminating case) Eg, if (n < 1) return 1; Otherwise, infinite loop. – Inductive case At kth step, we assume 1, …,k-1th steps are all correct. Recursive leap of faith

11
Recursion Recursive programming is NOT for optimizing performance. – But it helps: Organize the code better. Design the algorithms for complex problems.

12
Problem Partitioning Define the unit steps of the whole problem. – Your recursive function deals with one step. Once evaluated, the sub-solution for one step must remain valid through the whole procedure.

13
Problem Partitioning Define the unit steps of the whole problem. – Your recursive function deals with one step. Once evaluated, the sub-solution for one step must remain valid through the whole procedure. – How do you split the whole (or current) problem into sub-problems? the First and the Rest, or Even split (eg, left and right), or …

14
First & Rest Write a function to determine if the given array contains the given integer.

15
First & Rest Write a function to determine if the given array contains the given integer. – Given an array, we check the first element.

16
First & Rest Write a function to determine if the given array contains the given integer. – Given an array, we check the first element. – And we check the rest elements a[1 ~ n-1]. – But that is another array: (a+1) – So we can use the same function.

17
First & Rest Write a function to determine if the given array contains the given integer. We dont need to worry about the rest – They will be taken care of by recursive function calls.

18
Even Split Merge-sort

19
Even Split Merge-sort

20
Even Split Merge-sort How do you merge? Commonly used by many sorting algorithms

21
HW3

Similar presentations

OK

Chapter 12 Recursion, Complexity, and Searching and Sorting

Chapter 12 Recursion, Complexity, and Searching and Sorting

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on bmc remedy service Ppt on hard disk drive download Ppt on various categories of software Ppt on south indian cuisine Ppt on regular expression in java Ppt on obesity and nutrition Ppt on you can win as long as you keep Ppt on condition monitoring training Ppt on power systems download Ppt on balanced diet