Sorting Algorithms (Ch. 6 - 8) !
Sorting Algorithms (Ch. 6 - 8) !
Slightly modified definition of the sorting problem:! •! A sorting algorithm is comparison-based if the only operation
we can perform on keys is to compare two keys.!
input: A collection of n data items <a1,a2,...,an> where ! •! A sorting algorithm is in place if only a constant number of
data item ai has a key, ki, drawn from a totally elements of the input array are ever stored outside the array.!
ordered set (e.g., ints, chars)!
Max-Heap! Min-Heap!
In the array representation of a max-heap, the root of the tree"
Min-heaps are commonly used for priority queues in event-driven !
is in A[1], and given the index i of a node, "
simulators.!
Parent(i) " " LeftChild(i)" " RightChild(i)" Parent(i) LeftChild(i) RightChild(i)
return ("i/2#) return 2i " return (2i + 1)" return ("i/2#) return (2i) return (2i + 1)
Max-heap property: A[Parent(i)] " A[i]! Min-heap property: A[Parent(i)] ! A[i]!
1 2 3 4 5 6 7 8 9 10 11 index" 1 2 3 4 5 6 7 8 9 10 11 index!
A! 20 18 15 11 10 9 6 2 4 5 3" keys" A! 2 3 5 4 6 9 11 10 15 20 18! keys!
1" 1!
n = 11! 20" 2!
height = 3! 2" 3" 2! 3!
18" 15" 3! 5!
4" 5" 6" 7" 4! 5! 6! 7!
11" 10" 9" 6" 4! 6! 9! 11!
8" 9" 10" 11"
2" 4" 5" 3" 8! 10! 9! 15! 20!10! 18! 11!
Heap Sort! Heap Sort!
Input: An n-element array A[1…n] (unsorted).! Input: An n-element array A (unsorted).!
Output: An n-element array A in sorted order, smallest to largest.! Output: An n-element array A in sorted order, smallest to largest.!
HeapSort(A)! HeapSort(A)!
1. Build-Max-Heap(A) /* put all elements in heap */! 1. Build-Max-Heap(A) /* put all elements in heap */!
2. for i $ length(A) downto 2! 2. for i $ length(A) downto 2!
3. ! do swap A[1] % A[i] /* puts max in ith array position */! 3. ! do swap A[1] % A[i] /* puts max in ith array position */!
4. ! heap-size[A] $ heap-size[A] - 1! 4. ! heap-size[A] $ heap-size[A] - 1!
5. ! Max-Heapify(A,1) /* restore heap property */! 5. ! Max-Heapify(A,1) /* restore heap property */!
Running time:! We'll see that ! Running time of HeapSort!
Line 1:! Need to know running time of Build-Max-Heap! •! 1 call to Build-Max-Heap()!
•! Build-Max-Heap(A) takes !
Line 2: c2(length(A)) = c2n! ! & O(n) time!
Line 3:! c3(n - 1)! O(|A|) = O(n) time! •! n-1 calls to Max-Heapify()!
Line 4: !c4(n - 1)! •! Max-Heapify(A,1) takes ! each takes O(lgn) time!
Line 5:! Need to know running time of Max-Heapify! ! & O(nlgn) time!
O(lg|A|) = O(lgn) time!
•! The book uses a more complex analysis to show that Build- Let H be the height of the tree. If the heap is not a complete binary tree
Max-Heap runs in O(n) time. Since the running time of (because the bottom level is not full), then the nodes at a given depth
HeapSort is dominated by n calls to Max-Heapify, we will don’t all have the same height. Eg., although all the nodes with depth H
have height 0, the nodes with depth H-1 may have either height 0 or 1.
skip that analysis.
T!
Lemma 1: The number of internal nodes in a proper
binary tree is equal to the number of leaves in the
tree - 1.
Defn: In a proper binary tree (pbt), each node has exactly 0 or 2 children.
T1! T2!
Let I be the number of internal nodes and let L be the number of leaves in
a proper binary tree. The proof is by induction on the height of the tree.
#Internal nodes in T = #Internal nodes in T1 + #Internal nodes in T2 + 1!
Basis: h=0. I = 0 and L = 1. I = L - 1 = 1 - 1 = 0, so the lemma holds. " " = (#Leaves in T1 - 1) + (#Leaves in T2 -1) + 1 (IHOP)!
" " = (#Leaves in T1 + #Leaves in T2) - 2 + 1!
Inductive Step: Assume true for proper binary trees of height h (IHOP) " " =" #Leaves in T - 1 (by observation that # of!
and show for proper binary trees of height h + 1. " " " " " leaves in T is equal to # !
" " " " " leaves in its subtrees.)!
Consider a proper binary tree T of height h+1. It has left and right
subtrees (L and R) of height at most h.
IT = (IL + IR) + 1 = (LL - 1) + (LR - 1) + 1 (by the IHOP) =
(LL + LR -2) + 1 = LL + LR -1. Since LT=LL + LR we have that IT = LT - 1.
Theorem 1: The number of nodes at level h in a max- Theorem 1: The number of nodes at level h in a max-
heap ! )n/2h+1* heap ! )n/2h+1*
Let H be the height of the heap. Proof is by induction on h, the height of If n is odd, x is even, so all nodes have siblings (all internal nodes have 2
each node. The number of nodes in the heap is n. children.) By Lemma 1, the number of internal nodes = the number of
leaves - 1.
Basis: Show the thm holds for nodes with h = 0. The tree leaves (nodes
at height 0) are at depths H and H-1. So n = # of nodes = #of leaves + #internal nodes = 2(#of leaves) - 1.
Thus, the #of leaves = (n+1)/2 = )n/20+1* because n is odd.
Let x be the number of nodes at depth H, that is, the number of leaves
assuming that n is a complete binary tree, i.e. that n = 2h+1-1 Thus, the number of leaves = )n/20+1* and the thm holds for the base case.
Note that n-x is odd, because a complete binary tree has an odd number of
nodes (1 less than a power of 2).
Theorem 1: The number of nodes at level h in a max-
heap ! )n/2h+1*
Since the time of Max-Heapify when called on a node of height h is O(h),
the time of B-M-H is
Inductive step: Show that if the thm holds for height h-1, it holds for h. lg n lg n
n h
Let nh be the number of nodes at height h in the n-node tree T.
"2 h +1
O(h) = O(n "
2 h
)
h= 0 h= 0