Docsity

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

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.