Heap Sort and Quick Sort-2
Heap Sort and Quick Sort-2
Quicksort
Tarunpreet Bhatia
Lecturer, CSED, Thapar University
Sorting Revisited
14 10
8 7 9 3
2 4 1
14 10
8 7 9 3
A = 16 14 10 8 7 9 3 2 4 1 =
2 4 1
Heaps
14 10
8 7 9 3
A = 16 14 10 8 7 9 3 2 4 1 =
2 4 1
Referencing Heap Elements
So…
Parent(i) { return i/2; }
Left(i) { return 2*i; }
right(i) { return 2*i + 1; }
An aside: How would you implement this
most efficiently?
Trick question, I was looking for “i << 1”, etc.
But, any modern compiler is smart enough to do
this for you (and it makes the code hard to follow)
The Heap Property
Max-Heaps also satisfy the max-heap property:
A[Parent(i)] A[i] for all nodes i > 1
In other words, the value of a node is at most the
value of its parent
Where is the largest element in a max-heap stored?
Definitions:
The height of a node in the tree = the number of
edges on the longest downward path to a leaf
The height of a tree = the height of its root
What is the height of an n-element heap? Why?
This is nice: basic heap operations take at most
time proportional to the height of the heap
The depth of a node X in a tree T is defined as the length of
the simple path (number of edges) from the root node of T to
X.
Number of edges/arc from the root node to that node is called
as the Depth of that node.
Number of edges/arc from the root node to the leaf node of
the tree is called as the Depth of the Tree.
The level of a node is defined by 1 + the number of
connections between the node and the root.
The height of a tree is equal to the max depth of a tree.
The depth of a node and the height of a node are not
necessarily equal.
What are the minimum and maximum number of elements in a
heap of height h?
Show that n-element heap has height lg n
Is an array that is in sorted order a min-heap?
Is the sequence (23, 17, 14, 6, 13, 10, 1 , 5, 7, 12) a max-
heap?
Show that, with the array representation for storing an n-
element heap, the leaves are indexed by n/2 + 1, n/2 +
2 ……. n.
Show that there are at most n/2 h+1 nodes of height h in any
n-element heap.
Heap Operations: Max-Heapify()
16
4 10
14 7 9 3
2 8 1
A = 16 4 10 14 7 9 3 2 8 1
Max-Heapify()Example
16
4 10
14 7 9 3
2 8 1
A = 16 4 10 14 7 9 3 2 8 1
Max-Heapify() Example
16
4 10
14 7 9 3
2 8 1
A = 16 4 10 14 7 9 3 2 8 1
Max-Heapify()Example
16
14 10
4 7 9 3
2 8 1
A = 16 14 10 4 7 9 3 2 8 1
Max-Heapify() Example
16
14 10
4 7 9 3
2 8 1
A = 16 14 10 4 7 9 3 2 8 1
Max-Heapify() Example
16
14 10
4 7 9 3
2 8 1
A = 16 14 10 4 7 9 3 2 8 1
Max-Heapify() Example
16
14 10
8 7 9 3
2 4 1
A = 16 14 10 8 7 9 3 2 4 1
Max-Heapify() Example
16
14 10
8 7 9 3
2 4 1
A = 16 14 10 8 7 9 3 2 4 1
Max-Heapify() Example
16
14 10
8 7 9 3
2 4 1
A = 16 14 10 8 7 9 3 2 4 1
Analyzing Max-Heapify(): Informal
So we have
T(n) T(2n/3) + (1)
By case 2 of the Master Theorem,
T(n) = O(lg n)
Thus, Max-Heapify() takes logarithmic
time
Heap Operations: BuildMaxHeap()
1 3
2 16 9 10
14 8 7
Analyzing BuildMaxHeap()
Sorts in place
Sorts O(n lg n) in the average case
Sorts O(n2) in the worst case
So why would people use it instead of merge
sort?
Quicksort