Lecture 5 - Sorting and Order Statistics
Lecture 5 - Sorting and Order Statistics
Algorithms
Master Theorem
If a < bd
T(n) = If a = bd
If a > bd
Master Theorem: Pitfalls
❖Max Heap
Store data in ascending order
Has property of
A[Parent(i)] ≥ A[i]
❖Min Heap
Store data in descending order
Has property of
A[Parent(i)] ≤ A[i]
Heaps
❖The heap property requires that 45
each node's key is >= to the
keys of its children. 35 23
27 21 22 4
19
❖The biggest node is always at the top in a Maxheap.
Therefore, a heap can implement a priority queue
(where we need quick access to the highest priority
item).
Max Heap Example
19
12 16
1 4 7
19 12 16 1 4 7
Array A
Min heap example
4 16
7 12 19
1 4 16 7 12 19
Array A
Heapify
❖ Heapify picks the largest child key and compares it to the parent
key. If parent key is larger then heapify quits, otherwise it swaps
the parent key with the largest child key. So that the parent is
larger than its children.
HEAPIFY (A,i)
p LEFT(i) -- assign left index to p
if p<=heap-size[A] and A[p] > A[i] -- check left child and if larger than parent assign
then largest p -- largest= index of left child
if q<=heap-size[A] and A[q] > A[largest] -- check right child and if larger than parent or left child
then largest p -- largest= index of right child
BUILD-HEAP (A)
Heap-size [A] length [A]
for ilength[A/2] to 1
do HEAPIFY (A,i)
Running Time:
❖ Each call to HEAPIFY costs O(lg n)
❖ There are n calls: O(n)
O(n lg n)
Adding a Node to a Heap
35 23
27 21 22 4
19 42
Adding a Node to a Heap
45
35 23
45
42 21 22 4
42 23
19 27
35 21 22 4
19 27
Adding a Node to a Heap
45
❑Move the last node
onto the root.
42 23
35 21 22 4
19 27
Removing the Top of a Heap
42
❖ The process of pushing the new
node downward is called
reheapification downward. 35 23
27 21 22 4
19
Reheapification downward can stop under two
circumstances:
❖ The children all have keys <= the out-of-place node, or
❖ The node reaches the leaf.
Implementing a Heap
An array of data
Implementing a Heap
42
35 23
27 21
An array of data
42
27 21
42 35 23
An array of data
Implementing a Heap
27 21
42 35 23 27 21
An array of data
Heap Sort
HEAPSORT(A)
BUILD-HEAP (A)
for i length[A] down to 2
do exchange A[1] with A[i]
heap-size[A] heap-size[A]-1 --discard node n from heap by
decrementing
HEAPIFY (A,1)
Heapsort
4 7
1 12 19
Convert the array to a heap
16 16
4 7 4 19
swap
12 19 1 12 7
1
16 19
swap
12 19 12 16
swap
4 7 1 4 7
1
Time Analysis of Heapsort
4 5 2 3 1 1 2 3 4 5
❖ Performance: ❖ Performance:
insert takes O(1) time
since we can insert the insert takes O(n) time
item at the beginning or since we have to find the
end of the sequence place where to insert the
removeMin and min take item
O(n) time since we have removeMin and min take
to traverse the entire O(1) time, since the
sequence to find the smallest key is at the
smallest key
beginning
Insertion-Sort
Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (4,7)
(c) (2,5,3,9) (4,7,8)
(d) (5,3,9) (2,4,7,8)
(e) (3,9) (2,4,5,7,8)
(f) (9) (2,3,4,5,7,8)
(g) () (2,3,4,5,7,8,9)
Phase 2
(a) (2) (3,4,5,7,8,9)
(b) (2,3) (4,5,7,8,9)
.. .. ..
. . .
(g) (2,3,4,5,7,8,9) ()
In-place Insertion-sort
1 2 3 4 5
Real-life Applications