Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Heap

313 views

Published on

Heap sort

Published in: Education
  • Be the first to comment

Heap

  1. 1. 12/08/2006 CMSC 131 Fall 2006 Rance Cleaveland ©2006 Univeristy of Maryland Lecture 41: Heapsort Last time: 1. Analyzing selection sort 2. Trees Today: 1. Project #8 assigned! 2. Heaps 3. Heapsort
  2. 2. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 1 Recall: Binary Trees Data structures consisting of Nodes (where data it stored) Edges Every node has one parent except root, which has none Every node has ≤ two children Nodes with no children = leaves Nodes with children = internal Nodes can be divided into levels based on distance from root The height of a tree is the longest path from root A binary tree is complete if: Every level except the last is full In the last level, every leaf is as far to the left as possible A binary tree is perfect if every level (including the last) is full ‘a’ ‘y’ ‘J’ ‘3’‘c’
  3. 3. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 2 The Tree Quiz 1. Let T be a perfect tree of height n a. How many leaves does T have? 2n b. How many internal nodes does T have? 2n - 1 c. How many total nodes does T have? 2n+1 - 1 2. Let T be a complete binary tree of height n a. How many leaves does T have (give range)? 2n-1 to 2n, inclusive b. How many total nodes may T have (give range)? 2n to 2n+1 - 1, inclusive 3. Let T be a complete binary tree with n nodes. What is its height? Floor (rounding down) of log2n ‘a’ ‘y’ ‘J’ ‘3’‘c’ ‘z’ ‘:’
  4. 4. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 3 Heaps Assume data stored in tree nodes is ordered by ≤ A heap is: A complete binary tree such that: Every parent’s data is ≥ its child(ren)’s data Fact: greatest value is stored at root! 17 14 11 1015
  5. 5. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 4 Heapsort Idea: Keep unsorted elements in a heap Keep sorted elements in array, filling in from right (largest) to left (smallest) Maintain following property: Every unsorted element ≤ every sorted element (cf. selection sort) To increase sorted part: Remove root (largest element) from heap and put into correct position in array Restore remaining heap elements into a heap Questions How do you “restore” a heap? How do you create a heap in the first place? 17 14 11 1015 Unsorted elements 242219 Sorted elements
  6. 6. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 5 Restoring a Heap After removing root of a heap … … how do we rearrange the remainder into a heap? One approach: Move bottom-most, right- most leaf to root Repeatedly swap node with greater of children until heap property restored 17 14 11 1015
  7. 7. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 6 Example 11 14 1015 14 11 1015 15 14 1011 15 11 1014
  8. 8. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 7 What Is Complexity of Restoring a Heap? Each swap operation takes O(1) Swap always results in level of “out-of-place” node increasing How many times can level increase, in terms of n = # of nodes in tree? log2n! So complexity of restoration operation is O(log2n)
  9. 9. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 8 Creating a Heap To create a heap, we repeatedly insert elements from original array into heap How to insert a new node into a heap? Put new node into heap as bottom-most, right- most leaf Swap node with parent until heap property restored
  10. 10. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 9 Example: Inserting 13 17 14 11 1015 13 17 11 1315 1014
  11. 11. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 10 Complexity of Insertion Each swap is O(1) Swap decreases level of new node by one So complexity of insertion in terms of n = # of nodes in heap is: O(log2n)
  12. 12. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 11 Complexity of Heap Creation Recall heap-creation procedure (a is array to be sorted) start with empty heap h for i = 0, …, a.length insert a[i] into h Complexity analysis Insertion called for each element in a (so, n times) Each insertion costs O(log2n) So complexity of heap creation is O(n log2n)
  13. 13. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 12 Complexity of Heapsort Here is heapsort pseudo-code for sorting array a Create heap h from a for j = a.length-1, …, 0 do remove root from h and assign to a[j] restore heap Complexity analysis Heap creation: O(n log2n) Each loop iteration: O(log2n) Number of iterations: n Total: O(n log2n)!
  14. 14. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 13 0 1,000 2,000 3,000 4,000 5,000 6,000 7,000 0 8 16 24 32 40 48 56 64 72 80 Selection sort Heapsort Comparing Sorts # of elements in array runningtime
  15. 15. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 14 How To Implement Heaps? The heap can also be stored in a, along with sorted portion Root is stored in a[0] Level 1 is stored in a[1], a[2] Level 2 is stored in a[3] – a[6] etc. Using this scheme: Children of node stored in a[i] are a[2i+1], a[2i+2] Parent of node stored in a[i] is a[(i-1) / 2] 17 14 11 1015 242219 2422191114101517 heap sorted

×