Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort

2 A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition: At every node in a heap, the node value is >= all the values in its subtrees. A heap with heap_size elements can be represented as an array segment: A[1..heap_size]

3 3 Ordering of Nodes 16 1410 8 7 9 3 241 1 2 3 4 5 67 89 Each node is filled in order from left to right. All rows are filled except, possibly, the last row. Address of left child of node i => 2i Address of right child of node i => 2i+1 Address of parent of node i => Example: Left(4) = 8 Right(4) = 9 Parent(4) = 2

4 4 Heap properties 1.At every node in the heap, that node's value is greater than or equal to all the values in the subtrees of that node. 2.Heaps can be represented as an array. The elements in the array occur in the order of their heap addresses. A= 16 14 10 8 7 9 3 2 4 1 Note that the array is not sorted. Can guarantee that A[1] is the maximum value in the array.

5 5 Building a Heap _ To perform a heap sort, we must first build a heap out of our unsorted array. _To build a heap, we will use the function heapify, which creates a heap from a node i and two subtrees that are already heaps.

6 6 Heapify 16 314 15 6 9 11 782 1 i=2 3 4 5 67 89 10 Example: i = 2 Want to move the value, 3, to a position consistent with the heap property. In English: 1.Compare the value at node i with the values at each child node. Swap the value at node i with the largest of those values. 2.Repeat with the subtree that ith value was swapped into. Note: If the value at i is in the correct place, it will be swapped with itself and the algorithm is done.

7 7 Pseudocode for Heapify Heapify (A, i) lft  Left(i) rt  Right(i) if lft A[i] then largest  lft else largest  i if rt A[largest] then largest  rt if largest != i then Swap( A, i, largest ); Heapify (A, largest);

8 8 Final Tree from example 16 1514 8 6 9 11 732 1 i=2 3 4 5 67 89 10 Can show that heapify runs in O(lgn) time. (Why?)

9 9 Building a Heap 1.Each leaf of a binary tree is already a heap of size 1. 2.Build-Heap will start with the highest addressed, non-leaf node and heapify it. It then repeats with each node addressed 1 less than the previous node. 3.In a binary tree, the leaf nodes have addresses: We want to heapify starting with node: Build-Heap(A) heap-size  length[A] for i  length[A] / 2 downto 1 do Heapify(A, i)

10 10 Example A= 4 7 16 3 27 9 10 34 1 25 4 716 3 27 9 10 34125 1 2 3 4 5 67 89 10 We will work this out in class.

11 11 Heap Sort In English: Build a Heap out of the elements of the array We know that the maximum value is at A[1], so we swap it with the last element of the heap. Reduce the size of the heap by 1. Heapify the new, smaller heap. Repeat this process until all the nodes have been sorted.

12 12 Pseudocode for Heapsort Heapsort(A) Build-Heap(A) for i  length[A] downto 2 do Swap(A, 1, i) heap-size[A]  heap-size[A] - 1 Heapify(A, 1) What is the running time of Heapsort? Must find running times of Build-Heap and Heapify first.


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort."

Similar presentations


Ads by Google