Heap Implementation and Sorting Algorithms: Merge Sort and Heap Sort, Slides of Advanced Algorithms

An overview of sorting algorithms, specifically heap sort, merge sort, and their algorithmic complexities. It also covers priority queues and their implementation using heaps. Diagrams and explanations of insertion and down-heap bubbling processes.

Typology: Slides

2011/2012

Uploaded on 07/17/2012

padmajai
padmajai 🇮🇳

4.4

(12)

84 documents

1 / 91

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP202
Complexity of Algorithms
Sorting
[See Sections 2.4, 4.1, 4.3, and 4.5
in Goodrich and Tamassia.]
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b

Partial preview of the text

Download Heap Implementation and Sorting Algorithms: Merge Sort and Heap Sort and more Slides Advanced Algorithms in PDF only on Docsity!

COMP

Complexity of Algorithms

Sorting

[See Sections 2.4, 4.1, 4.3, and 4.

in Goodrich and Tamassia.]

Learning outcomes

  1. Understand the sorting problem, and its fundamental importance in algorithms.
  2. Have a wide knowledge of different types of sorting algorithms available (such as heap sort, merge sort, and quick sort), as well as their algorithmic complexity (i.e. running time).
  3. Comprehend the idea behind the divide-and-conquer methods of merge-sorting and quick sorting.
  4. Be able to explain the mechanism behind the randomized quick sort algorithm, and why randomization is used.

Sorting (cont.)

Sorting is a fundamental algorithmic problem in computer science.

Many algorithms perform sorting (as a subroutine) during their execution. Hence, efficient sorting methods are crucial to achieving good algorithmic performance.

We will investigate various methods that we can use to sort items. Why several methods?

Sorting (cont.)

We may not always require a fully sorted list (for example), so some methods might be more appropriate depending upon the exact task at hand.

Sorting algorithms might be directly adaptable to perform additional tasks and directly provide solutions in this fashion.

Priority Queue - Sorting

How can we use a priority queue to perform sorting on a set C? Do this in two phases: I (^) First phase: Put elements of C into an initially empty priority queue, P, by a series of n insertItem operations. I (^) Second phase: Extract the elements from P in non-decreasing order using a series of n removeMin operations.

PQ Sorting - Algorithm

PQ-SORT(C, P)

 Input: An n element sequence C and a priority queue P.  Output: The sequence C sorted using the total order relation. 1 while C 6 = ∅ 2 do 3 e ← C.REMOVEFIRST() 4 P.INSERTITEM(e, e) 5 while P 6 = ∅ 6 do 7 e ← P.REMOVEMIN() 8 C.INSERTLAST(e)

Heap-order property

I (^) In a heap T , for every node v (excluding the root) the key at v is greater than (or equal to) the key stored at its parent.

4 5 6

15 7 20

16 25 14 15 11 8

9

PQ/Heap implementation

An efficient realization of a heap can be achieved using an array for storing the elements (i.e. the vector representation of a tree that we discussed earlier). So a heap can be implemented with the following: I (^) heap: A (nearly complete) binary tree T containing elements with keys satisfying the heap-order property, stored in an array. I (^) last: A reference to the last used node of T in this array representation. I (^) comp: A comparator function that defines the total order relation on keys and which is used to maintain the minimum (or maximum) element at the root of T.

Insertion in heaps

Inserting a new item into a heap begins by adding this element to the bottom of the tree in the position of the first unused, or empty, child.

Then, if necessary, this new element “bubbles” its way up the heap until the heap-order property is restored.

Up-heap bubbling (insertion)

(4,C) (6,Z) (7,Q) (11,S) (8,W)

(20,B)

(5,A) (9,F) (14,E) (^) (12,H)

(16,K) (16,X) (^) (25,J)

(a)

(4,C) (6,Z) (7,Q) (11,S) (8,W)

(5,A) (9,F) (14,E) (^) (12,H)

(16,K) (16,X) (^) (25,J)

(20,B) (2,T)

(b) (4,C) (6,Z) (7,Q) (11,S) (8,W)

(5,A) (9,F) (14,E) (^) (12,H)

(16,K) (16,X) (^) (25,J)

(2,T) (20,B)

(c)

Deletion in a heap

Deletion in a heap consists of removing the minimum (or maximum) element (at the root) from the heap. Then the bottom, right-most element in the heap (the element at the end of the array that stores the heap) is moved to the root. To restore the heap-order property, this item then “sinks” or bubbles down the heap.

Down-heap bubbling (removal of top element)

(9,F) (14,E) (^) (12,H)

(5,A) (16,K) (16,X) (^) (25,J)

(20,B)

(4,C)

(6,Z) (7,Q) (11,S) (8,W)

(9,F) (14,E) (^) (12,H)

(5,A) (16,K) (16,X) (^) (25,J)

(20,B)

(6,Z) (7,Q) (11,S)

(4,C) (8,W)

(a)

(b) docsity.com

Heap performance

Since a heap is an almost-complete binary tree, it stores n items in a tree of height O(log n). Thus we have the following summary of running times for operations that can be performed on a heap.

Operation time size, isEmpty O( 1 ) minElement, minKey O( 1 ) insertItem O(log n) removeMin O(log n)

Heap-Sorting

Theorem : The heap-sort algorithm sorts a sequence, S, of n comparable items in O(n log n) time, where I (^) Bottom-up construction of heap with n elements takes O(n log n) time, and I (^) Extraction of n elements (in increasing order) from the heap takes O(n log n) time.