CS106X – Programming Abstractions in C++
description
Transcript of CS106X – Programming Abstractions in C++
![Page 1: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/1.jpg)
CS106X – Programming Abstractions in C++Cynthia Bailey Lee
CS2 in C++ Peer Instruction
Materials by Cynthia Bailey Lee is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Permissions beyond the scope of this license may be available at
http://peerinstruction4cs.org.
![Page 2: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/2.jpg)
2
Today’s Topics: Heaps!1. Binary heaps
Insert, delete Reasoning about outcomes in Binary
heaps, and Big O analysis2. Heapsort algorithm3. (Schedule note: we’ll save stack and
queue implementation for another day…)
![Page 3: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/3.jpg)
Binary heap insert and delete
![Page 4: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/4.jpg)
Binary heap insert + “bubble up”
![Page 5: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/5.jpg)
Binary heap insert
![Page 6: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/6.jpg)
Binary heap delete + “trickle-down”
![Page 7: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/7.jpg)
Binary heap delete + “trickle-down”
![Page 8: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/8.jpg)
Heap outcomes by insert orderCreate a MIN heap by inserting the elements, one by one, in the order given below for the second letter of your first name:
A-F: {3, 9, 18, 22, 34}
G-L: {3, 22, 18, 9, 34}
M-R: {9, 22, 18, 3, 34}
S-Z: {18, 22, 3, 9, 34}
![Page 9: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/9.jpg)
TRUE OR FALSE There is only one configuration of a valid
min-heap containing the elements {34, 22, 3, 9, 18}
A. TRUEB. FALSE
![Page 10: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/10.jpg)
Heap outcomes by insert orderCreate a MIN heap by inserting the elements, one by one, in the order given below for the first letter of your last name:
A-F: {18, 9, 34, 3, 22}
G-L: {3, 18, 22, 9, 34}
M-R: {22, 9, 34, 3, 18}
S-Z: {34, 3, 9, 18, 22}
![Page 11: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/11.jpg)
How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34}?A. 1-2B. 3-4C. 5-8D. 5! (5 factorial)E. Other/none/more
![Page 12: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/12.jpg)
Time cost What is the worst-case time cost for
each heap operation: Add, Remove, Peek?
A. O(n), O(1), O(1)B. O(logn), O(logn), O(1)C. O(logn), O(1), O(logn)D. O(n), O(logn), O(logn)E. Other/none/more
![Page 13: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/13.jpg)
Heapsort
![Page 14: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/14.jpg)
Heapsort is super easy1. Insert unsorted elements one at a time
into a heap until all are added2. Remove them from the heap one at a
time (we will always be removing the next biggest item, for max-heap; or next smallest item, for min-heap)
THAT’S IT!
![Page 15: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/15.jpg)
Implementing heapsortDevil’s in the details
![Page 16: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/16.jpg)
Build max-heap by inserting elements one at a time:
![Page 17: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/17.jpg)
Build max-heap by inserting elements one at a time:
What is the next configuration in this sequence?
A.12, 8, 2, 10B.12, 10, 2, 8C.12, 10, 8, 2D.Other/none/more
than one
![Page 18: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/18.jpg)
Build max-heap by inserting elements one at a time:
![Page 19: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/19.jpg)
Sort array by removing elements one at a time:
![Page 20: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/20.jpg)
Build heap by inserting elements one at a time IN PLACE:
![Page 21: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/21.jpg)
Sort array by removing elements one at a time IN PLACE:
![Page 22: CS106X – Programming Abstractions in C++](https://reader036.fdocuments.net/reader036/viewer/2022062501/568162a0550346895dd315cd/html5/thumbnails/22.jpg)
Complexity How many times do we do insert() when
building the heap?
How much does each cost?
How many times do we delete-max() when doing the final sort?
How much does each cost?