Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 225 Lecture 16 – Heap Sort.

Similar presentations


Presentation on theme: "CMPT 225 Lecture 16 – Heap Sort."— Presentation transcript:

1 CMPT 225 Lecture 16 – Heap Sort

2 Last Lecture We saw how to … Define binary heaps: minimum and maximum
Demonstrate and trace their recursive operations Describe Priority Queue Define public interface of Priority Queue ADT Design and implement Priority Queue ADT using various data structures Compare and contrast these various implementations using Big O notation Give examples of real-life applications (problems) where we could use Priority Queue ADTs to solve the problem Solve problems using Priority Queue ADT

3 Learning Outcomes At the end of the next few lectures, a student will be able to: Define the following data structures: Binary search tree Balanced binary search tree (AVL) Binary heap as well as demonstrate and trace their operations Implement the operations of binary search tree and binary heap Implement and analyze sorting algorithms: tree sort and heap sort Write recursive solutions to non-trivial problems, such as binary search tree traversals and heap sort

4 Today’s menu Our goal is to Understand how heap sort works
Sort an array using heap sort Analyze time/space efficiency of heap sort

5 Remember Selection Sort
Sorting algorithm in which, at every iteration, we “selected” the smallest (or largest) element of the unsorted section of our array and swap it into the sorted section of our array

6 Heap Sort Sorting algorithm in which, at every iteration, we “select” the smallest or min (largest or max) element of the unsorted section of our array and swap it into the sorted section of our array Use minimum binary heap in order to sort an array of data in descending order Use maximum binary heap in order to sort data in ascending order

7 Overview of Heap Sort Algorithm
To sort an array of n elements: Interpret the array as a binary tree in contiguous storage Such a tree is always complete But it may not be a heap Phase 1: Convert the binary tree (array) into a heap, i.e., heapify! Phase 2: Sort the heap The heap sort algorithm consists of two phases

8 Heap Sort Algorithm Heapify Sort Phase 1
Let index be the index of the last parent node in the tree While index >= reHeapDown( index ) index -- Phase 2 (heap can be seen as the unsorted section of array) Set counter unsorted to n (unsorted -> size of heap) Store the last element of the heap into temporary storage lastElement Move the root to the last position in the heap Decrease counter unsorted to exclude the last entry from further sorting (can be seen as the first element in sorted section of array) Insert lastElement into root (position now available) reHeapDown( indexOfRoot ) between positions 0 and unsorted – 1 Repeat steps 2 to 6 until unsorted is 1 Heapify Sort

9 ReHeapDown( ) (Min heap)
reHeapDown( indexOfRoot ) // If the parent has at least a child and … if ( heap[indexOfRoot] is not a leaf ) compute index of children compare the children and pick smallest child set indexOfMinChild to index of smallest child of root // … key value of parent > key value of smallest child then … if ( heap[indexOfRoot] > heap[indexOfMinChild] ) //… swap the parent with its smallest child swap heap[indexOfRoot] with heap[indexOfMinChild] reHeapDown( indexOfMinChild )

10 ReHeapDown( ) (Max heap)
reHeapDown( indexOfRoot ) // If the parent has at least a child and … if ( heap[indexOfRoot] is not a leaf ) compute index of children compare the children and pick largest child set indexOfMaxChild to index of largest child of root // … key value of parent < key value of largest child then … if ( heap[indexOfRoot] < heap[indexOfMaxChild] ) //… swap the parent with its largest child swap heap[indexOfRoot] with heap[indexOfMaxChild] reHeapDown( indexOfMaxChild )

11 Demo of Heap Sort - 1 Phase 1

12 Demo of Heap Sort - 2 n lastElement

13 Demo of Heap Sort - 3

14 Let’s try!

15 Time Complexity Analysis of Heap Sort
Phase 1 O(n) Phase 2 O( n log2 n ) Hence in the worst case the overall time complexity of the heap sort algorithm is: max[ O(n), O( n log2 n ) ] = O( n log2 n )

16 Space Complexity Analysis of Heap Sort
How much space (memory) does heap sort require to execute (aside from original data collection)? Therefore, its space efficiency is ->

17 Heap Sort is not Stable

18 Careful with Step 1 of Heap Sort
Goal of Step 1 of Heap Sort is to transform an array into a heap To do so, we could Heapify Or we could _________________________________________ Is there a difference?

19 √ Learning Check We can now … Understand how heap sort works
Sort an array using heap sort Analyze time/space efficiency of heap sort

20 Next Lecture Binary Search Tree


Download ppt "CMPT 225 Lecture 16 – Heap Sort."

Similar presentations


Ads by Google