Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Adding a Node to (Min) Heap Step 1: Add node at the end Step 2: Make swap if parent is bigger Step 3: Continue to swap if parent is bigger 2
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Delete a Node from (Min) Heap Step 1: Remove root node Step 2: Move last node to root Step 3: Make swap if child is smaller Step 4: Make swap if child is smaller 3
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Priority Queue Sort We can use a priority queue to sort a set of elements. Insert the elements one by one with a series of insert operations. Remove the elements in sorted order with a series of deleteMax operations. 4 Algorithm PQ‐Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P priority queue with comparator C while S.isEmpty () e S.removeFirst () P.insert (e) while P.isEmpty() e P.removeMax().getKey() S.addLast(e)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Priority Queue Sort using a Heap 5 public void heapSort(int[] A) { Heap H = new Heap(); for (int i = 0; i < A.length; i++) H.insert(A[i]); for (int i = 0; i < A.length; i++) A[i] = H.deleteMax(); }
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Convert an unordered array into a heap 6
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Shift up from the root if necessary 7
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 8
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 9
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 10
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 11
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 12
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 13
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 14
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 15
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 16
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 17
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 18
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 19
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 20
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 21
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 22
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Given: an unsorted array Objective: sort the array Make unsorted array a heap Easy to find the maximum (minimum) element in heap Discard the maximum (minimum) element, easy to find the maximum (minimum) element in the remaining heap Repeat until all elements are sorted. In‐place algorithm 23
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 1: Make array a heap. 24
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 1: Make array a heap. 25
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 2: Swap the maximum and last number in the heap. 26
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 3: Discard the last item in the heap and heapify the remaining array. 27
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure 28 Step 2: Swap the maximum and last number in the heap.
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure 29 Step 3: Discard the last item in the heap and heapify the remaining array.
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Repeat swap and heapify operations. 30
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Repeat swap and heapify operations. 31
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort 32
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Faster Heap‐Sort 33 2 i 1 2i112i11
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merging Two Heaps 34
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Merging 35
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Time Complexity Comparison Comparison to other sorting algorithms 36
37