CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE2 Week 14: Lecture 28, April 24. 2019 Slides updated: 3:29pm, April 24, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA https://www.cs.purdue.edu/homes/apm/courses/CS180_Java/CS180Spring2019/
©Aditya Mathur. CS 180. Fall 2019.Week 15 Today Binary trees Quicksort A good bye message 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree T3 T1 T2 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree: Elements Root Nodes Left link Right link Leaf nodes 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©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/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©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/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree: traversal Inorder: Left, Root, Right 12 8 -2 11 17 99 3 -2 3 8 11 12 17 99 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 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 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Quicksort: Illustration of partitioning i=-1 j=0 Pivot i=-1; j=0 Compare 15 with the pivot (8) [15 3 12 2 8] 15>8; array not changed j=1; i=-1; compare 3 with 8. [15 3 12 2 8] 3<8; bring 3 to position 0 j=2; i=0; compare 19 with 8 [3 15 12 2 8] 12>8; no change j=3; i=0; compare 2 with 8 [3 15 12 2 8] 2<8; move 2 to i+1 j=4; i=1; [3 15 12 2 8] Exchange pivot with 12 j=4; i=2; [3 2 12 15 8] Exchange pivot with 12 j=4; i=2; [3 2 8 15 12] 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Quicksort: Apply quicksort on two subarrays [3 2 8 15 12] [3 2 ] [PLeft] Quicksort [15 12] [PRight] Quicksort 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Programming Alan Perlis, the first ACM Turing Award laureate, argued in 1961 that all university students take a course in "computers,'' and that they should all learn programming [...]. The process of preparing programs for a digital computer is especially attractive, not only because it can be economically and scientifically rewarding, but also because it can be an aesthetic experience much like composing poetry or music. —DONALD E. KNUTH, The Art of Computer Programming: Fundamental Algorithms, Vol. 1 (2nd ed., 1973, p. v) 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
Computers are beautiful things. You have learned how to communicate with them. Enjoy life! Best wishes for success in whatever you do and where ever you go! 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15
©Aditya Mathur. CS 180. Fall 2019.Week 15 Quiz: 04/24/2019 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15