Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS Problem Solving and Object Oriented Programming Spring 2019

Similar presentations


Presentation on theme: "CS Problem Solving and Object Oriented Programming Spring 2019"— Presentation transcript:

1 CS 18000 Problem Solving and Object Oriented Programming Spring 2019
Section LE2 Week 14: Lecture 27, April Slides updated: 9:21am, April 22, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA

2 ©Aditya Mathur. CS 180. Fall 2019.Week 15
This week Recursion Some mathematical recursive functions Tower of Hanoi Binary trees Quicksort 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

3 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Recursion Using a function or a method to evaluate itself! Some problems have a easier to find recursive than an iterative solution. In some cases a recursive solution may turn out to be more expensive than an iterative solution. Thus, use recursion when the problem demands it! 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

4 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Sample recursive functions Fibonnacci(n)= 1; if n=0 or 1; =Fibonnaci(n-1)+Fibonnaci(n-2); if n>1; Fibonnaci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34…. fact(n)= 1; if n=0 =n*fact(n-1) if n>0 GCD(x,y)= x; if x=y =GCD(x-y,y); x>y =GCD(x,y-x); x<y 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

5 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Tower of Hanoi A C B N discs in tower A Smaller disc must always be above a larger disc; arranged in order Move all discs from A to C via B. Only one disc can be moved at a time and must be placed on a tower and not anywhere else. 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

6 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Tower of Hanoi: Solution 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

7 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Live demo 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

8 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Binary tree T3 T1 T2 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

9 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Binary tree: Elements Root Nodes Left link Right link Leaf nodes 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

10 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Binary tree: with data Data: 12, 8, -2, 11, 17, 99, 3 12 8 17 11 99 -2 3 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

11 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Binary tree: traversal Inorder: Left, Root, Right 12 8 -2 11 17 99 3 Preorder: Root, Left, Right Postorder: Left, Right, Root Level order: Level by level 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

12 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Quiz: 04/22/2019 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

13 Quicksort: The algorithm
Problem: Sort an array: data=[d0 d1 d2 d3 dN ] Quicksort(data, 0, N) Select an element in the array; this is element is known as the pivot. Partition the array so that the left partition (Pleft) has elements less than pivot and the right partition (Pright) has elements equal to or greater than the pivot. Apply Quicksort( ) to Pleft and Pright

14 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Quicksort: Illustration of partitioning j=0 Pivot i=-1 i=-1; j=0 Compare 15 with the pivot (8) [ ] 15>8; array not changed J=1; i=-1; compare 3 with 8. [ ] 3<8; bring 3 to position 0 J=2; i=0; compare 19 with 8 [ ] 19>8; no change J=3; i=0; compare 2 with 8 [ ] 2<8; move 2 to i+1 J=4; i=1; [ ] Exchange pivot with 19 J=4; i=2; [ ] Exchange pivot with 19 J=4; i=2; [ ] 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

15 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Quicksort: Apply quicksort on two subarrays [ ] [3 2 ] Quicksort [ ] Quicksort 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15


Download ppt "CS Problem Solving and Object Oriented Programming Spring 2019"

Similar presentations


Ads by Google