



















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 91
This page cannot be seen from the preview
Don't miss anything!




















































































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?
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.
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.
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)
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
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.
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.
(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 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.
(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)
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)
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.