Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heapsort By Pedro Oñate CS-146 Dr. Sin-Min Lee. Overview: Uses a heap as its data structure In-place sorting algorithm – memory efficient Time complexity.

Similar presentations


Presentation on theme: "Heapsort By Pedro Oñate CS-146 Dr. Sin-Min Lee. Overview: Uses a heap as its data structure In-place sorting algorithm – memory efficient Time complexity."— Presentation transcript:

1 Heapsort By Pedro Oñate CS-146 Dr. Sin-Min Lee

2 Overview: Uses a heap as its data structure In-place sorting algorithm – memory efficient Time complexity – O(n log(n))

3 What is a Heap? A heap is also known as a priority queue and can be represented by a binary tree with the following properties: Structure property: A heap is a completely filled binary tree with the exception of the bottom row, which is filled from left to right Heap Order property: For every node x in the heap, the parent of x greater than or equal to the value of x. (known as a maxHeap).

4 Example: 53 4425 15211318 31257 a heap

5 Algorithm Step 1. Build Heap – O(n) - Build binary tree taking N items as input, ensuring the heap structure property is held, in other words, build a complete binary tree. - Heapify the binary tree making sure the binary tree satisfies the Heap Order property. Step 2. Perform n deleteMax operations – O(log(n)) - Delete the maximum element in the heap – which is the root node, and place this element at the end of the sorted array.

6 Simplifying things For speed and efficiency we can represent the heap with an array. Place the root at array index 1, its left child at index 2, its right child at index 3, so on and so forth… 53 4425 15211318 31257 0 1 2 3 4 5 6 7 8 9 10 11 5344251521131831257

7 53 4425 15211318 31257 0 1 2 3 4 5 6 7 8 9 10 11 5344251521131831257 For any node i, the following formulas apply: The index of its parent = i / 2 Index of left child = 2 * i Index of right child = 2 * i + 1

8 Sample Run Start with unordered array of data Array representation: Binary tree representation: 21152535127194529 21 1525 35127 194529

9 Sample Run Heapify the binary tree - 21 1525 35127 194529 21 1525 39127 194525 21 1525 459127 19325 21 1525 459127 19325 21 4525 199127 15325 45 2125 199127 15325

10 Step 2 – perform n – 1 deleteMax(es), and replace last element in heap with first, then re-heapify. Place deleted element in the last nodes position. 45 2125 199127 15325 45212519912715325 25 2112 19957 1532 25211219957153245 25 2112 19957 1532 25211219957153245 21 1912 15957 23 21191215957232545

11 21 1912 15957 23 21191215957232545 19 1512 3957 2 19151239572212545 19 1512 3957 2 19151239572212545 15 912 3257 15912325719212545

12 15 912 3257 15912325719212545 12 97 325 973251519212545 12 97 325 973251519212545 9 57 32 95732121519212545 … and finally 2 23579121519212545

13 Conclusion 1 st Step- Build heap, O(n) time complexity 2 nd Step – perform n deleteMax operations, each with O(log(n)) time complexity total time complexity = O(n log(n)) Pros: fast sorting algorithm, memory efficient, especially for very large values of n. Cons: slower of the O(n log(n)) sorting algorithms


Download ppt "Heapsort By Pedro Oñate CS-146 Dr. Sin-Min Lee. Overview: Uses a heap as its data structure In-place sorting algorithm – memory efficient Time complexity."

Similar presentations


Ads by Google