1 / 44

Heap Sort

Heap Sort. Quicksort. The order of magnitude of average key comparisons is 1.39 n log 2 n + O(n)  O( n log 2 n) The order of magnitude of average key swaps is 0.69 n log 2 n + O(n)  O( n log 2 n)

Download Presentation

Heap Sort

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. Heap Sort

  2. Quicksort • The order of magnitude of average key comparisons is 1.39n log2 n + O(n)  O( n log2 n) • The order of magnitude of average key swaps is 0.69n log2 n + O(n)  O( n log2 n) • The order of magnitude of average key assignments is 2 x (0.69n log2 n + O(n))  O( n log2 n)

  3. Heapsort • Heapsort sorts a contiguous list of length n with O(n log2 n)comparisons and movements of entries, even in its worst case. • Better than Quicksort O(n2). • Better than Mergesort, since it does not need any extra space.

  4. Heapsort • The order of magnitude of worst-case key comparisons is 2n log2 n + O(n)  O( n log2 n) • The order of magnitude of worst-case key assignments is n log2 n + O(n)  O( n log2 n)

  5. Heapsort • Heapsort based on a tree structure that reflects the hierarchical order of a corporate management in an organization.

  6. Figure 11-10

  7. Heap as a list • The root of the tree is in position 0 of the list, • then the left and right children of the node in position k are in positions 2k + 1and 2k + 2of the list, respectively. • If these positions are beyond the end of the list, then these children do not exist. k 2k+1 2k+2

  8. Heapsort • A heap is a list in which each entry contains a key, and, for all positions k in the list, the key at position k is at least as large as the keys in positions 2k and 2k + 1, provided these positions exist in the list. • A heap is defintely not an ordered list.

  9. Figure 11-11

  10. Heaps

  11. Heaps

  12. Recall that . . . A heap is a binary tree that satisfies these special SHAPE and ORDER properties: • Its shape must be a complete binary tree. • For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

  13. 70 The largest element in a heap is always found in the root node root 12 60 40 30 8 10

  14. 70 0 30 4 10 6 The heap can be stored in an array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5

  15. Heap Sort Approach • First, make the unsorted array into a heap by satisfying the order property. Then repeat the steps below until there are no more unsorted elements. • Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements. • Reheap the remaining unsorted elements. (This puts the next-largest element into the root position).

  16. 70 0 30 4 10 6 After creating the original heap values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5

  17. 70 0 30 4 10 6 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5

  18. 10 0 30 4 After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 60 12 40 30 8 70 12 2 60 1 40 3 8 5 70 6 NO NEED TO CONSIDER AGAIN

  19. 60 0 30 4 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 8 70 12 2 40 1 10 3 8 5 70 6

  20. 60 0 30 4 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 8 70 12 2 40 1 10 3 8 5 70 6

  21. 8 0 30 4 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 8 40 12 10 30 60 70 12 2 40 1 10 3 60 5 70 6

  22. 40 0 6 4 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6

  23. 40 0 6 4 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6

  24. 6 0 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 30 12 10 40 60 70 30 1 12 2 10 3 60 5 70 6 40 4

  25. 30 0 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 30 10 12 6 40 60 70 10 1 12 2 6 3 60 5 70 6 40 4

  26. 30 0 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 30 10 12 6 40 60 70 10 1 12 2 6 3 60 5 70 6 40 4

  27. 6 0 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 10 12 30 40 60 70 10 1 12 2 30 3 60 5 70 6 40 4

  28. 12 0 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 12 10 6 30 40 60 70 10 1 6 2 30 3 60 5 70 6 40 4

  29. 12 0 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 12 10 6 30 40 60 70 10 1 6 2 30 3 60 5 70 6 40 4

  30. 6 0 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 10 12 30 40 60 70 10 1 12 2 30 3 60 5 70 6 40 4

  31. 10 0 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 6 12 30 40 60 70 6 1 12 2 30 3 60 5 70 6 40 4

  32. 10 0 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 6 12 30 40 60 70 6 1 12 2 30 3 60 5 70 6 40 4

  33. After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 10 12 30 40 60 70 6 0 10 1 12 2 30 3 60 5 70 6 40 4 ALL ELEMENTS ARE SORTED

  34. template <class ItemType > void HeapSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[ 0 . . numValues-1 ] into ascending // order by key { int index ; // Convert array values[ 0 . . numValues-1 ] into a heap. for ( index = numValues/2 - 1 ; index >= 0 ; index-- ) ReheapDown ( values , index , numValues - 1 ) ; // Sort the array. for ( index = numValues - 1 ; index >= 1 ; index-- ) { Swap ( values [0] , values [index] ); ReheapDown ( values , 0 , index - 1 ) ; } } 39

  35. ReheapDown template< class ItemType > void ReheapDown ( ItemType values [ ], int root, int bottom ) // Pre: root is the index of a node that may violate the heap // order property // Post: Heap order property is restored between root and bottom { int maxChild ; int rightChild ; int leftChild ; leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ; 40

  36. if ( leftChild <= bottom ) // ReheapDown continued { if ( leftChild == bottom ) maxChild = leftChild; else { if (values [ leftChild ] <= values [ rightChild ] ) maxChild = rightChild ; else maxChild = leftChild ; } if ( values [ root ] < values [ maxChild ] ) { Swap ( values [ root ] , values [ maxChild ] ) ; ReheapDown ( maxChild, bottom ) ; } } } 41

  37. Heap Sort: How many comparisons? 15 7 6 8 In reheap down, an element is compared with its 2 children (and swapped with the larger). But only one element at each level makes this comparison, and a complete binary tree with N nodes has only O(log2N) levels. root 24 0 60 1 12 2 40 4 8 5 10 6 30 3 18 9 70 10

  38. Heap Sort of N elements: How many comparisons? (N/2) * O(log N) compares to create original heap (N-1) * O(log N) compares for the sorting loop = O ( N * log N) compares total

  39. Hashing • is a means used to order and access elements in a list quickly -- the goal is O(1) time -- by using a function of the key value to identify its location in the list. • The function of the key value is called a hash function. FOR EXAMPLE . . .

  40. Using a hash function values HandyParts company makes no more than 100 different parts. But the parts all have four digit numbers. This hash function can be used to store and retrieve parts in an array. Hash(key) = partNum % 100 [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] . . . Empty 4501 Empty 8903 8 10 7803 Empty . . . Empty 2298 3699 [ 97] [ 98] [ 99]

  41. Priority Queues • Another application of heap. • A priority queue consists of entries, each of which contains a key called the priority of the entry. • A priority queue has only two operations other than the usual creation, clearing, size, full, and empty operations: • Insert an entry. • Remove the entry having the largest (or smallest) key. • If entries have equal keys, then any entry with the largest key may be removed first.

  42. Priority Queues • In a time-sharing computer system, for example, a large number of tasks may be waiting for the CPU. Some of these tasks have higher priority than others. Hence the set of tasks waiting for the CPU forms a priority queue. • Other applications of priority queues include simulations of time-dependent events (like the airport simulation in Section 3.5) and solution of sparse systems of linear equations by row reduction.

  43. Priority Queues • We could represent a priority queue as a sorted contiguous list, in which case removal of an entry is immediate, but insertion would take time proportional to n, the number of entries in the queue. • Or we could represent it as an unsorted list, in which case insertion is rapid but removal is slow.

  44. Priority Queues • Now consider the properties of a heap. • The entry with largest key is on the top and can be removed immediately. • It will, however, take time O(log n) to restore the heap property for the remaining keys. • If, however, another entry is to be inserted immediately, then some of this time may be combined with the O(log n) time needed to insert the new entry. • Thus the representation of a priority queue as a heap proves advantageous for large n, since it is represented efficiently in contiguous storage and is guaranteed to require only logarithmic time for both insertions and deletions.

More Related