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

Post on 17-Dec-2015

220 views 1 download

Tags:

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

Lecture16: Heap Sort

Bohyung HanCSE, 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.

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.

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)

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

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

Insertion Heapifying of an Array

• Convert an unordered array into a heap

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

Insertion Heapifying of an Array

• Shift up from the root if necessary

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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

Insertion Heapifying of an Array

• Continue to shift up until the last node

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‐

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

Heap Sort Procedure‐

• Step 1: Make array a heap.

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

Heap Sort Procedure‐

• Step 1: Make array a heap.

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

Heap Sort Procedure‐

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

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.

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

Heap Sort Procedure‐

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

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.

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

Heap Sort Procedure‐

• Repeat swap and heapify operations.

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

Heap Sort Procedure‐

• Repeat swap and heapify operations.

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

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

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

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: ‐

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

Time Complexity Comparison

• Comparison to other sorting algorithms

37