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

37
Lecture16: Heap Sort Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

Transcript of Lecture16: Heap Sort Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

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

Lecture16: Heap Sort

Bohyung HanCSE, POSTECH

[email protected]

CSED233: Data Structures (2014F)

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

2 CSED233: Data Structuresby 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

Takes steps when there are nodes.

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

3 CSED233: Data Structuresby 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

Takes steps when there are nodes.

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

4 CSED233: Data Structuresby 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.

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)

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

5 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Priority Queue Sort using a Heap

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

𝑂 (𝑛 log𝑛)

𝑂 (𝑛 log𝑛)

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

6 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Convert an unordered array into a heap

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

7 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Shift up from the root if necessary

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

8 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

9 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

10 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

11 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

12 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

13 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

14 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

15 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

16 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

17 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

18 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

19 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

20 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

21 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

22 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

23 CSED233: Data Structuresby 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‐

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

24 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Step 1: Make array a heap.

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

25 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Step 1: Make array a heap.

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

26 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Step 2: Swap the maximum and last number in the heap.

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

27 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Step 3: Discard the last item in the heap and heapify the remaining array.

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

28 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Step 2: Swap the maximum and last number in the heap.

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

29 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Step 3: Discard the last item in the heap and heapify the remaining array.

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

30 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Repeat swap and heapify operations.

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

31 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort Procedure‐

• Repeat swap and heapify operations.

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

32 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Heap Sort‐

• Consider a priority queue with items implemented by means of a heap Space complexity: Methods insert and deleteMax: Methods size, isEmpty, and max:

• Using a heap based priority queue,‐ We can sort a sequence of elements time. The resulting algorithm is called heap sort.‐ Heap sort is much faster than quadratic sorting algorithms, such as ‐

insertion sort and selection sort.‐ ‐

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

33 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

A Faster Heap Sort‐

• Time complexity of insertion Insert keys one by one taking times. If we know all keys in advance, we can save the construction to

times by bottom up construction

• Bottom up construction‐ We can construct a heap storing given keys with phases. In phase , a pair of heaps with keys are merged into a heap

with keys.

2i -1 2i -12i+1-1

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

34 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Merging Two Heaps

• Procedure Given two heaps and a key , we create a new heap with the root node

storing and with the two heaps as subtrees. We perform downheap to restore the heap-order property

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

35 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Analysis of Merging

• Shift down with a proxy path‐ Goes right first and then repeatedly goes left until the leaf of the heap Note: This path may differ from the actual shift down path.‐ Each edge is traversed by at most one proxy path:

• Time complexity Successive insertions: Bottom up construction: ‐

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

36 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Time Complexity Comparison

• Comparison to other sorting algorithms

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

37