1 / 15

Final (important!) details: Uphead & Downheap Java details see code provided for practical 9

Final (important!) details: Uphead & Downheap Java details see code provided for practical 9. Heap Sort. Remember bubble-sort from L16? A simple PQ-based sorting algorithm called “heap sort”: Insert all items to be sorted into a Priority Queue Remove them; they’ll be in sorted order.

shelly
Download Presentation

Final (important!) details: Uphead & Downheap Java details see code provided for practical 9

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. Final (important!) details: Uphead & Downheap Java details • see code provided for practical 9

  2. Heap Sort • Remember bubble-sort from L16? • A simple PQ-based sorting algorithm called “heap sort”: • Insert all items to be sorted into a Priority Queue • Remove them; they’ll be in sorted order [4,19,10,3,22,13,8,21,20,28,7,25] cost = ncost(insert) = nO(log(n)) cost = ncost(remove) = nO(log(n)) [3,4,7,8,10,13,19,20,21,22,25,28] total cost = 2nO(log(n)) = O(n  log(n))

  3. Heap Sort - analysis • All heap methods runin O(log(n)) time, andwe need n insertions &n removals. Thereforetotal time is O(2n log(n))= O(n log(n)) • Compare to bubblesort’s O(n2) complexity

  4. Speeding up heap-sort a bit [4,19,10,3,22,13,8,21,20,28,7,25] cost = ncost(insert) = nO(log(n)) cost = O(n) cost = ncost(remove) = nO(log(n)) [3,4,7,8,10,13,19,20,21,22,25,28] total cost of revised algorithm = O(n) + O(n log(n)) = O(n log(n)) total cost of original algorithm = O(n log(n))

  5. Slight improvement, not detected by complexity analysis revised = O(n) + O(n log(n)) = O(n log(n)) original = 2*n*O(log(n)) = O(n log(n)) Revised algorithm is slightly faster … but if you’re sortinga million numbers you need to squeeze out every dropof efficiency

  6. Bottom-Up Heap Construction • To build heap from [16,25,9,12,11,8,23,27,15,5,7,20,4,6,14]… • 1. build (n+1)/2 trivial one-element heaps from ~half of the elements • 2.a build 3-element heaps on top from ~half of remaining elements

  7. Bottom-Up Heap Construction • 2.b downheap to preserve the order property • 3.a form seven-element heaps

  8. Bottom-Up Heap Construction (cont.) • 3.b downheap to preserve the order property • 4.a add root element

  9. Bottom-Up Heap Construction (cont.) • 4.b downheap to preserve the order property

  10. Analysis of Bottom-Up Heap Construction • Proposition: Bottom-up heap constructionwith n keys takes O(n) time. Proof: Let the leaf path associated witha node be the right-left-left-left-…path from the node to a leaf. The length of a node’s leaf pathis an upper bound on the cost of bottom-up step for that node. Therefore sum of lengths of leaf paths for all nodes is an upper boundon the total cost of the bottom-up construction algorithm. Since each edge participates in exactly one leaf-path, the sum of the lengths of all leaf-paths is equal to the number of edges in the tree. If the tree has n nodes then it has at most 2n edges. Therefore thecost up the bottom-up algorithm is O(2n) = O(n).

  11. AlmostTheEnd

  12. Review of COMP-2001 • Programming • What’s an “if” statement? …….. Recursion • OO design basics (inheritance, overloading, …) • What is well-designed software (abstraction, encapsulation, modularity, …) • OO constructs that help you design & build good software (interfaces, class hierarchy, public/private, …) • A (small) sample of useful generic data structures • Stack, Queue, Deque • Vector, List, Sequence • Tree, BinaryTree • Priority queues

  13. Review, continued • Some simple and general (but non-obvious!) design “tricks” for tying all these data-types together • Position • Iterator • top-level “Container” class • Comparator ADT • Implementation techniques • Arrays • Linked structures • Algorithm analysis tools • pseudo code, primitive unit-cost operations • Big-O notation • Algorithms • Sorting (bubble-sort) & heap-sort) • Tree height, depth, traversal

  14. Review, continued • Preparing for the exam • Book & its web site have hundreds more sample problems • Practice writing lots and lots and lots of programs:Learning computer science -- even the “formal” stuff -- is more like learning to fix a car than learning philosophy. Just reading the book can lull you into a false confidence. Instead, pick a program you want to write; when you get stuck; read the book to figure out where to go next. • Although course contents have been rearranged substantially, previous exams are an excellent model for your exam. (Except of course for the content that we didn’t cover, which should(!) be obvious!)

  15. TheEnd

More Related