CS 18000 Problem Solving and Object Oriented Programming
CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE 2 Week 14: Lecture 28, April 24. 2019 Slides updated: 3: 29 pm, April 24, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA https: //www. cs. purdue. edu/homes/apm/courses/CS 180_Java/CS 180 Spring 2019/
Today Binary trees Quicksort A good bye message 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 2
Binary tree T 3 T 1 T 2 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 3
Binary tree: Elements Root Left link Nodes Right link Leaf nodes 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 4
Binary tree: with data Data: 12, 8, -2, 11, 17, 99, 3 12 8 17 11 -2 99 3 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 5
Binary tree: traversal Inorder: Left, Root, Right 12 Preorder: Root, Left, Right 8 17 11 -2 Postorder: Left, Right, Root 99 Level order: Level by level 3 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 6
Binary tree: traversal Inorder: Left, Root, Right 12 8 11 -2 -2 3 8 11 12 17 99 3 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 7
Quicksort: The algorithm Problem: Sort an array: data=[d 0 d 1 d 2 d 3 d. N ] 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 8
Quicksort: Illustration of partitioning i=-1 Pivot j=0 [15 3 12 2 8] 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. [3 15 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 2 12 15 8] Exchange pivot with 12 j=4; i=2; [3 2 8 15 04/24/2019 Exchange pivot with 12 j=4; i=2; ©Aditya Mathur. CS 180. Fall 2019. Week 12] 15 9
Quicksort: Apply quicksort on two subarrays [3 2] [PLeft] Quicksort [15 12] [PRight] Quicksort 04/24/2019 8 15 12] ©Aditya Mathur. CS 180. Fall 2019. Week 15 10
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 (2 nd ed. , 1973, p. v) 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 11
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 12
Quiz: 04/24/2019 ©Aditya Mathur. CS 180. Fall 2019. Week 15 13
- Slides: 13