heap sort - ursinus collegewebpages.ursinus.edu/adhawan/algorithms/heapsort.pdf• max-heapify(a, i)...
TRANSCRIPT
![Page 1: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/1.jpg)
Heap Sort
![Page 2: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/2.jpg)
Properties
• Running time = O(n lg n)
• In place sorting
• Uses a data structure called a “heap”
![Page 3: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/3.jpg)
Heaps
• Nearly complete binary tree
• Can be represented as a tree/array
• Two-types of heaps
– MAX-HEAPS– MAX-HEAPS
– MIN-HEAPS
![Page 4: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/4.jpg)
Heap Property
• Max-Heap Property
For every node other than the root
A[PARENT(i)] ≥ A[i]
Largest element in the heap is the rootLargest element in the heap is the root
• Min-Heap Property
A[PARENT(i)] ≤ A[i]
Smallest element in the heap is the root
![Page 5: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/5.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 6: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/6.jpg)
Representation
• In an array representation, computing child
and parent locations are very efficient
PARENT(i)PARENT(i)
return └i/2┘
LEFT(i) RIGHT(i)
return 2i return 2i + 1
• Height – longest path from root to leaf
![Page 7: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/7.jpg)
Use
• Max Heaps – Heap Sort
• Min Heaps – Priority Queue’s
• Before looking at Heap Sort we will look at
some operations on a heap
![Page 8: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/8.jpg)
Maintaining the heap property
• MAX-HEAPIFY(A, i)
– A is the array
– i is an index in the array
• Assumption is that the binary trees rooted at • Assumption is that the binary trees rooted at
LEFT(i) and RIGHT(i) are max-heaps but A[i]
may be smaller than its children
• This violates the heap property and we want
to rearrange the elements to reinforce a heap
![Page 9: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/9.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 10: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/10.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 11: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/11.jpg)
Complexity of MAX-HEAPIFY
• As an index i, MAX-HEAPIFY can take as long
as the largest subtree of i
• Depends on height O(h)
• W.C. Root is wrong, ,O(lg n)• W.C. Root is wrong, ,O(lg n)
![Page 12: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/12.jpg)
Building a Heap
• Given an arbitrary array A[1..n] create a heap
• Run MAX-HEAPIFY in a bottom up manner
• Leaves are already 1-element heaps
• Leaves – A[(└n/2┘+1) … n]• Leaves – A[(└n/2┘+1) … n]
![Page 13: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/13.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 14: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/14.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 15: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/15.jpg)
Complexity of Building a Heap
• Intuition
• Each call to MAX-HEAPIFY cost O(lg n)
• O(n) such calls, running time O(n lg n)
• But this is not a tight bound!• But this is not a tight bound!
![Page 16: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/16.jpg)
Heap Sort
• Root (A[1]) of a Max Heap is largest element
• We can put it in its correct position by
swapping it with A[n]
• But this destroys the Heap property• But this destroys the Heap property
• MAX-HEAPIFY(A,1) restores this
• Repeat from n-1 to a heap size of 1
![Page 17: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/17.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 18: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/18.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 19: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/19.jpg)
Complexity of Heap Sort
• BUILD-HEAP – O(n)
• n-1 calls to MAX-HEAPIFY each taking O(lg n)
• Total O(n lg n)
![Page 20: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/20.jpg)
Priority Queues
• Quick Sort usually defeats Heap Sort
• But a Heap is a very useful data structure
• A big use of Heaps – Efficient Priority Queues
• 2 types• 2 types
– Max-priority Queue
– Min-priority Queue
![Page 21: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/21.jpg)
Max-Priority Queues
• INSERT(S,x) inserts the element x into set S.
S <- S U {x}
• MAXIMUM(S) returns max key of S
• EXTRACT-MAX(S) removes and returns max • EXTRACT-MAX(S) removes and returns max
key of S
• INCREASE-KEY(S, x, k) increases element x’s
key to k where k >= x
• Similar operations for Min-Priority Queues
![Page 22: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/22.jpg)
HEAP-MAXIMUM (A)
• Θ(1)
![Page 23: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/23.jpg)
HEAP-EXTRACT-MAX(A)
• O(lg n)
![Page 24: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/24.jpg)
HEAP-INCREASE-KEY(A, i, key)
• Traverse from node to root to find the right
place – O(lg n)
![Page 25: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/25.jpg)
Copyright © The McGraw-Hill Companies, Inc. Permiss ion required for reproduction or display.
![Page 26: Heap Sort - Ursinus Collegewebpages.ursinus.edu/adhawan/algorithms/HeapSort.pdf• MAX-HEAPIFY(A, i) – A is the array – iis an index in the array • Assumption is that the binary](https://reader033.vdocuments.us/reader033/viewer/2022042911/5f41dd83ecc3aa10a805de02/html5/thumbnails/26.jpg)
MAX-HEAP-INSERT(A, key)
• Expand max heap by adding a leaf (-infinity)
• Then call HEAP-INCREASE-KEY
• O(lg n)