Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Final (important!) details: Uphead & Downheap Java details see code provided for practical 9."— Presentation transcript:

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

2 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] [3,4,7,8,10,13,19,20,21,22,25,28] cost = n  cost(insert) = n  O(log(n)) cost = n  cost(remove) = n  O(log(n)) total cost = 2  n  O(log(n)) = O(n  log(n))

3 3 Heap Sort - analysis All heap methods run in O(log(n)) time, and we need n insertions & n removals. Therefore total time is O(2n log(n)) = O(n log(n)) Compare to bubble sort’s O(n 2 ) complexity

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

5 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 sorting a million numbers you need to squeeze out every drop of efficiency

6 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 7 Bottom-Up Heap Construction 2.b downheap to preserve the order property 3.a form seven-element heaps

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

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

10 10 Analysis of Bottom-Up Heap Construction Proposition: Bottom-up heap construction with n keys takes O(n) time. Proof: Let the leaf path associated with a node be the right-left-left-left-… path from the node to a leaf. The length of a node’s leaf path is 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 bound on 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 the cost up the bottom-up algorithm is O(2n) = O(n).

11 11 Almost The End

12 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 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 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 15 The End


Download ppt "1 Final (important!) details: Uphead & Downheap Java details see code provided for practical 9."

Similar presentations


Ads by Google