1 / 22

CS106X – Programming Abstractions in C++

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 .

tosca
Download Presentation

CS106X – Programming Abstractions in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. CS106X – Programming Abstractions in C++ Cynthia Bailey Lee

  2. Today’s Topics: Heaps! • Binary heaps • Insert, delete • Reasoning about outcomes in Binary heaps, and Big O analysis • Heapsort algorithm • (Schedule note: we’ll save stack and queue implementation for another day…)

  3. Binary heap insert and delete

  4. Binary heap insert + “bubble up”

  5. Binary heap insert

  6. Binary heap delete + “trickle-down”

  7. Binary heap delete + “trickle-down”

  8. Heap outcomes by insert order Create 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}

  9. TRUE OR FALSE • There is only one configuration of a valid min-heap containing the elements {34, 22, 3, 9, 18} • TRUE • FALSE

  10. Heap outcomes by insert order Create 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}

  11. How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34}? • 1-2 • 3-4 • 5-8 • 5! (5 factorial) • Other/none/more

  12. Time cost • What is the worst-case time cost for each heap operation: Add, Remove, Peek? • O(n), O(1), O(1) • O(logn), O(logn), O(1) • O(logn), O(1), O(logn) • O(n), O(logn), O(logn) • Other/none/more

  13. Heapsort

  14. Heapsort is super easy • Insert unsorted elements one at a time into a heapuntil all are added • 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!

  15. Implementing heapsort Devil’s in the details

  16. Build max-heap by inserting elements one at a time:

  17. Build max-heap by inserting elements one at a time: What is the next configuration in this sequence? 12, 8, 2, 10 12, 10, 2, 8 12, 10, 8, 2 Other/none/more than one

  18. Build max-heap by inserting elements one at a time:

  19. Sort array by removing elements one at a time:

  20. Build heap by inserting elements one at a time IN PLACE:

  21. Sort array by removing elements one at a time IN PLACE:

  22. 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?

More Related