150 likes | 424 Views
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.
E N D
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 [4,19,10,3,22,13,8,21,20,28,7,25] cost = ncost(insert) = nO(log(n)) cost = ncost(remove) = nO(log(n)) [3,4,7,8,10,13,19,20,21,22,25,28] total cost = 2nO(log(n)) = O(n log(n))
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
Speeding up heap-sort a bit [4,19,10,3,22,13,8,21,20,28,7,25] cost = ncost(insert) = nO(log(n)) cost = O(n) cost = ncost(remove) = nO(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))
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
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
Bottom-Up Heap Construction • 2.b downheap to preserve the order property • 3.a form seven-element heaps
Bottom-Up Heap Construction (cont.) • 3.b downheap to preserve the order property • 4.a add root element
Bottom-Up Heap Construction (cont.) • 4.b downheap to preserve the order property
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).
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
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
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!)