previous sorting algorithms g g
play

Previous sorting algorithms g g Insertion Sort O(n 2 ) time - PowerPoint PPT Presentation

Previous sorting algorithms g g Insertion Sort O(n 2 ) time Heapsort Heapsort Merge Sort Based off slides by: David Matuszek Based off slides by: David Matuszek http://www.cis.upenn.edu/~matuszek/cit594-2008/ O(n) space P


  1. Previous sorting algorithms g g � Insertion Sort � O(n 2 ) time Heapsort Heapsort � Merge Sort Based off slides by: David Matuszek Based off slides by: David Matuszek http://www.cis.upenn.edu/~matuszek/cit594-2008/ � O(n) space P Presented by: Matt Boggus t d b M tt B 2 Heap data structure p Balanced binary trees y � Recall: Binary tree � � The depth of a node is its distance from the root Th d th f d i it di t f th t � The depth of a tree is the depth of the deepest node Balanced � � A binary tree of depth n is balanced if all the nodes at � A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children Left-justified j � n-2 (Max) Heap property: no node has a value greater (Max) Heap property: no node has a value greater � n-1 n 1 than the value in its parent n Balanced Balanced Not balanced 3 4

  2. Left-justified binary trees j y Building up to heap sort g p p � How to build a heap � A balanced binary tree of depth n is left- A balanced binary tree of depth n is left justified if: � it has 2 n nodes at depth n (the tree is “full”) or � it has 2 nodes at depth n (the tree is full ), or � How to maintain a heap � it has 2 k nodes at depth k , for all k < n , and all the leaves at depth n are as far left as possible t e eaves at dept a e as a e t as poss b e � How to use a heap to sort data p Left-justified Left justified Not left-justified Not left justified 5 6 The heap property p p p y siftUp � Given a node that does not have the heap property, you can � A node has the heap property if the value in the give it the heap property by exchanging its value with the give it the heap property by exchanging its value with the node is as large as or larger than the values in its d i l l h h l i i value of the larger child children 12 14 12 12 12 8 8 14 14 8 8 12 12 8 8 3 3 8 8 12 12 8 8 14 14 Blue node does not Blue node has Blue node has Blue node has Blue node does not have heap property have heap property heap property heap property heap property heap property heap property heap property have heap property have heap property � All leaf nodes automatically have the heap property � This is sometimes called sifting up � A binary tree is a heap if all nodes in it have the A bi t i h if ll d i it h th heap property 7 8

  3. Constructing a heap I g p Constructing a heap II g p � Each time we add a node, we may destroy the heap � A tree consisting of a single node is automatically property of its parent node property of its parent node a heap � To fix this, we sift up � We construct a heap by adding nodes one at a time: � But each time we sift up, the value of the topmost node � But each time we sift up the value of the topmost node � Add the node just to the right of the rightmost node in in the sift may increase, and this may destroy the heap the deepest level property of its parent node � If the deepest level is full, start a new level If the deepest level is full start a new level � We repeat the sifting up process, moving up in the tree, � Examples: until either � We reach nodes whose values don’t need to be swapped W h d h l d ’t d t b d Add a new Add a new (because the parent is still larger than both children), or node here node here � We reach the root 9 10 Constructing a heap III g p Other children are not affected 8 8 8 8 10 10 10 10 12 12 12 12 14 14 10 8 8 5 10 5 14 5 12 5 1 2 3 8 14 8 10 8 10 10 10 10 10 12 12 The node containing 8 is not affected because its parent gets larger, not � 8 5 12 5 10 5 smaller The node containing 5 is not affected because its parent gets larger, not � smaller 12 8 8 The node containing 8 is still not affected because, although its parent got � 4 smaller its parent is still greater than it was originally smaller, its parent is still greater than it was originally 11 12

  4. A sample heap p p Removing the root (animated) g ( ) � Here’s a sample binary tree after it has been heapified � Notice that the largest number is now in the root � Suppose we discard the root: � Suppose we discard the root: 25 11 22 17 22 22 17 17 19 22 14 15 19 19 22 22 14 14 15 15 18 14 21 3 9 11 18 14 21 3 9 11 � Notice that heapified does not mean sorted � How can we fix the binary tree so it is once again balanced and left-justified? � Heapifying does not change the shape of the binary tree; this binary tree is balanced and left-justified because it � Solution: remove the rightmost leaf at the deepest level and started out that way use it for the new root 13 14 The reHeap method I p The reHeap method II p � Our tree is balanced and left-justified, but no longer a heap � Now the left child of the root (still the number 11 ) lacks the heap property the heap property � However, only the root lacks the heap property � However only the root lacks the heap property 11 22 22 22 17 17 11 11 17 17 19 19 22 22 14 14 15 15 19 19 22 22 14 14 15 15 18 14 21 3 9 18 14 21 3 9 � We can siftUp() the root � We can siftUp() this node � After doing this one and only one of its children may have � After doing this, one and only one of its children may have � After doing this one and only one of its children may have � After doing this, one and only one of its children may have lost the heap property lost the heap property 15 16

  5. The reHeap method III The reHeap method IV p p � Now the right child of the left child of the root (still the � Our tree is once again a heap, because every node in it has number 11 ) lacks the heap property: number 11 ) lacks the heap property: the heap property the heap property 22 22 22 22 17 17 22 22 17 17 19 19 11 11 14 14 15 15 19 19 21 21 14 14 15 15 18 14 21 3 9 18 14 11 3 9 � We can siftUp() this node � Once again, the largest (or a largest) value is in the root � After doing this, one and only one of its children may have � After doing this one and only one of its children may have � We can repeat this process until the tree becomes empty � We can repeat this process until the tree becomes empty lost the heap property —but it doesn’t, because it’s a leaf � This produces a sequence of values in order largest to smallest 17 18 Sorting Key properties y p p � What do heaps have to do with sorting an array? � Here’s the neat part: H ’ h � Determining location of root and “last node” take � Because the binary tree is balanced and left justified, it can be constant time represented as an array p y � Danger Will Robinson: This representation works well only with balanced , left-justified binary trees � All our operations on binary trees can be represented as � All our operations on binary trees can be represented as � Remove n elements, re-heap each time operations on arrays � To sort: heapify the array; heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; h th t d } 19 20

  6. Analysis y Analysis y � To reheap the root node, we have to follow one path f from the root to a leaf node (and we might stop before h l f d ( d i h b f � Construct the heap O(n log n) we reach a leaf) � The binary tree is perfectly balanced Th bi t i f tl b l d � Therefore, this path is O(log n) long � Remove and re-heap O(n log n) � And we only do O(1) operations at each node l d O(1) A d ti t h d � Therefore, reheaping takes O(log n) times � Since we reheap inside a while loop that we do n times, � Since we reheap inside a while loop that we do n times � Total time O(n log n) + O(n log n) the total time for the while loop is n* O(log n) , or O(n log n) O(n log n) 21 22 The End Priority Queue y Q � Continue to priority queues? � Queue – only access element in front � Queue elements sorted by order of importance � Implement as a heap where nodes store priority values p p p y 23 24

  7. Extract Max Increase Key � Remove root � Change node value � Swap with last node � Re-heapify � Re-heapify p y 25 26 Insert � Add new node, priority is minimum possible value The End The End � Increase priority 27

Recommend


More recommend