Heapsort. Why study Heapsort?

Similar documents
BM267 - Introduction to Data Structures

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

MPATE-GE 2618: C Programming for Music Technology. Unit 4.3

Heaps. Heaps Priority Queue Revisit HeapSort

Heapsort. Heap data structure

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Heaps and Priority Queues

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Transform & Conquer. Presorting

9. Heap : Priority Queue

Data Structures and Algorithms for Engineers

Properties of a heap (represented by an array A)

Chapter 6 Heapsort 1

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Priority queues. Priority queues. Priority queue operations

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Priority Queues and Binary Heaps

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

Binary Heaps in Dynamic Arrays

Priority Queues. T. M. Murali. January 29, 2009

CS350: Data Structures Heaps and Priority Queues

Data Structures and Algorithms

CS 350 : Data Structures B-Trees

COS 226 Midterm Exam, Spring 2009

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

DATA STRUCTURES AND ALGORITHMS

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

Sorting and Searching

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Data Structures and Algorithms Chapter 4

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

CS350: Data Structures B-Trees

Lecture Notes on Priority Queues

Sorting and Searching

Data Structures and Algorithms. Roberto Sebastiani

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Binary heaps (chapters ) Leftist heaps

Midterm solutions. n f 3 (n) = 3

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Trees. A tree is a directed graph with the property

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Priority Queues, Binary Heaps, and Heapsort

Priority Queues Heaps Heapsort

Balanced Search Trees

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

CS102 Binary Search Trees

Data Structures and Algorithms

Heaps. Heaps. A heap is a complete binary tree.

Heap sort. Carlos Moreno uwaterloo.ca EIT

AVL Trees Heaps And Complexity

Algorithms. AVL Tree

Lecture 5: Sorting Part A

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Programming II (CS300)

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Data Structures Lesson 9

Binary Search Trees Treesort

Lower Bound on Comparison-based Sorting

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch]

Multiway Search Trees

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS165: Priority Queues, Heaps

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

Heaps Goodrich, Tamassia. Heaps 1

Lecture: Analysis of Algorithms (CS )

Algorithms and Data Structures

Data Structures and Algorithms Week 4

Heap sort. Carlos Moreno uwaterloo.ca EIT

Fundamental Algorithms

Collection of priority-job pairs; priorities are comparable.

Recall: Properties of B-Trees

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

Priority Queues and Heaps. Heaps of fun, for everyone!

Programming II (CS300)

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Binary Trees, Binary Search Trees

Priority Queues, Heaps, and Heapsort

Priority Queues Heaps Heapsort

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Programming II (CS300)

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

EST Solutions. Ans 1(a): KMP Algorithm for Preprocessing: KMP Algorithm for Searching:

Trees 1: introduction to Binary Trees & Heaps. trees 1

Topic: Heaps and priority queues

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Section 1: True / False (1 point each, 15 pts total)

Transcription:

Heapsort Material adapted courtesy of Prof. Dave Matuszek at UPENN Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort is usually O(n log n) but in the worst case slows to O(n 2 ) Quicksort is generally faster, but Heapsort is better in time-critical applications Heapsort is a really cool algorithm! 2 1

What is a heap? Definitions of heap: 1. A large area of memory from which the programmer can allocate blocks as needed, and deallocate them (or allow them to be garbage collected) when no longer needed 2. A balanced, left-justified binary tree in which no node has a value greater than the value in its parent These two definitions have little in common Heapsort uses the second definition Recall: Balanced binary trees The depth of a node is its distance from the root The depth of a tree is the depth of the deepest node A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children n-2 n-1 n Balanced Balanced Not balanced 4 2

Left-justified binary trees A balanced binary tree of depth n is leftjustified if: it has 2 n nodes at depth n (the tree is full ), or it has 2 k nodes at depth k, for all k < n, and all the leaves at depth n are as far left as possible Left-justified Not left-justified 5 Plan of attack First, we will learn how to turn a binary tree into a heap Next, we will learn how to turn a binary tree back into a heap after it has been changed in a certain way Finally (this is the cool part) we will see how to use these ideas to sort an array 6

The heap property A node has the heap property if the value in the node is as large as or larger than the values in its children 8 Blue node has heap property 8 Blue node has heap property 8 Blue node does not have heap property All leaf nodes automatically have the heap property A binary tree is a heap if all nodes in it have the heap property 7 siftup Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child 8 Blue node does not have heap property 8 Blue node has heap property This is sometimes called sifting up Notice that the child may have lost the heap property 8 4

Constructing a heap I A tree consisting of a single node is automatically a heap We construct a heap by adding nodes one at a time: Add the node just to the right of the rightmost node in the deepest level If the deepest level is full, start a new level Examples: Add a new node here Add a new node here Constructing a heap II Each time we add a node, we may destroy the heap property of its parent node To fix this, we sift up But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent node We repeat the sifting up process, moving up in the tree, until either We reach nodes whose values don t need to be swapped (because the parent is still larger than both children), or We reach the root 10 5

Constructing a heap III 8 8 10 10 10 8 8 5 1 2 10 10 8 5 5 10 5 8 8 4 Other children are not affected 10 5 5 5 8 8 10 8 10 The node containing 8 is not affected because its parent gets larger, not smaller The node containing 5 is not affected because its parent gets larger, not smaller The node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally 6

A sample heap Here s a sample binary tree after it has been heapified 25 1 Notice that heapified does not mean sorted Heapifying does not change the shape of the binary tree; this binary tree is balanced and left-justified because it started out that way 1 Removing the root Notice that the largest number is now in the root Suppose we discard the root: 1 We need to promote some other node to be the new root 7

Removing the root (animated) Here s our rootless binary tree: 1 How can we fix the binary tree so it is once again balanced and left-justified? Solution: remove the rightmost leaf at the deepest level and use it for the new root The reheap method I Our tree is balanced and left-justified, but no longer a heap However, only the root lacks the heap property 1 We can siftup() the root After doing this, one and only one of its children may have lost the heap property 16 8

The reheap method II Now the left child of the root (still the number ) lacks the heap property 1 We can siftup() this node After doing this, one and only one of its children may have lost the heap property The reheap method III Now the right child of the left child of the root (still the number ) lacks the heap property: 1 We can siftup() this node After doing this, one and only one of its children may have lost the heap property but it doesn t, because it s a leaf

The reheap method IV Our tree is once again a heap, because every node in it has the heap property 1 Once again, the largest (or a largest) value is in the root We can repeat this process until the tree becomes empty This produces a sequence of values in order largest to smallest 1 Sorting What do heaps have to do with sorting an array? Here s the neat part: Because the binary tree is balanced and left justified, it can be represented as an array Danger: This representation works well only with balanced, leftjustified binary trees All our operations on binary trees can be represented as operations on arrays To sort: heapify the array; while the array isn t empty { remove and replace the root; reheap the new root node; } 20 10

Mapping into an array 25 1 0 1 2 4 5 6 7 8 10 25 1 Notice: The left child of index i is at index 2*i+1 The right child of index i is at index 2*i+2 Example: the children of node (1) are 7 () and 8 () Removing and replacing the root The root is the first element in the array The rightmost node at the deepest level is the last element Swap them... 0 1 2 4 5 6 7 8 10 25 1 0 1 2 4 5 6 7 8 10 1 25...And pretend that the last element in the array no longer exists that is, the last index is (containing the value )

Reheap and repeat Reheap the root node (index 0, containing )... 0 1 2 4 5 6 7 8 10 1 25 0 1 2 4 5 6 7 8 10 1 25 0 1 2 4 5 6 7 8 10 1 25...And again, remove and replace the root node Remember, though, that the last array index is changed Repeat until the last becomes first, and the array is sorted! 2 Analysis I Here s how the algorithm starts: heapify the array; Heapifying the array: we add each of n nodes Each node has to be sifted up, possibly as far as the root Since the binary tree is perfectly balanced, sifting up a single node takes O(log n) time Since we do this n times, heapifying takes n*o(log n) time, that is, O(n log n) time 24

Analysis II Here s the rest of the algorithm: while the array isn t empty { remove and replace the root; reheap the new root node; } We do the while loop n times (actually, n-1 times), because we remove one of the n nodes each time Removing and replacing the root takes O(1) time Therefore, the total time is n times however long it takes the reheap method 25 Analysis III To reheap the root node, we have to follow one path from the root to a leaf node (and we might stop before we reach a leaf) The binary tree is perfectly balanced Therefore, this path is O(log n) long And we only do O(1) operations at each node Therefore, reheaping takes O(log n) times Since we reheap inside a while loop that we do n times, the total time for the while loop is n*o(log n), or O(n log n) 26 1

Analysis IV Here s the algorithm again: heapify the array; while the array isn t empty { remove and replace the root; reheap the new root node; } We have seen that heapifying takes O(n log n) time The while loop takes O(n log n) time The total time is therefore O(n log n) + O(n log n) This is the same as O(n log n) time 27 The End 28