Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Similar presentations


Presentation on theme: "Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"— Presentation transcript:

1 Lecture16: Heap Sort Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

2 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

3 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

4 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)

5 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(); }

6 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Convert an unordered array into a heap 6

7 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Shift up from the root if necessary 7

8 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 8

9 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 9

10 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 10

11 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 11

12 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 12

13 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 13

14 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 14

15 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 15

16 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 16

17 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 17

18 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 18

19 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 19

20 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 20

21 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 21

22 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Heapifying of an Array Continue to shift up until the last node 22

23 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

24 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 1: Make array a heap. 24

25 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 1: Make array a heap. 25

26 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Step 2: Swap the maximum and last number in the heap. 26

27 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

28 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure 28 Step 2: Swap the maximum and last number in the heap.

29 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.

30 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Repeat swap and heapify operations. 30

31 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort Procedure Repeat swap and heapify operations. 31

32 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Heap‐Sort 32

33 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Faster Heap‐Sort 33 2 i  1 2i112i11

34 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merging Two Heaps 34

35 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Merging 35

36 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Time Complexity Comparison Comparison to other sorting algorithms 36

37 37


Download ppt "Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"

Similar presentations


Ads by Google