Lecture - 11 - Randomized Quick Sort, Heap Sort
Lecture - 11 - Randomized Quick Sort, Heap Sort
2
Randomized Quick Sort - Example
• Let’s take a sorted list as an input for this example.
3, 5, 7, 8, 12, 15
Case-1 Case-2
Considering the worst case possible, if the random If the random pivot function chooses 7 as the pivot
pivot chosen is also the highest index number, it number. Now the pivot divides the list into half so
compares all the other numbers and another pivot is standard quick sort is carried out usually. However,
selected. Since 15 is greater than all the other the time complexity is decreased than the worst case.
numbers in the list, it won’t be swapped, and another
pivot is chosen.
3
What is a “heap”?
• Definitions of heap:
1. A large area of memory from which the programmer can
allocate blocks as needed, and deallocate them (or allow
them to be garbage collected) when no longer needed
2. A balanced, left-justified binary tree in which no node
has a value greater than the value in its parent
• These two definitions have little in common
• Heapsort uses the second definition
Why study Heapsort?
• It is a well-known, traditional sorting algorithm you will be expected
to know
• Heapsort is always O(n log n)
• Quicksort is usually O(n log n) but in the worst case slows to O(n2)
• Quicksort is generally faster, but Heapsort is better in time-critical
applications
• Heapsort is a really cool algorithm!
Balanced binary trees
• Recall:
• The depth of a node is its distance from the root
• The depth of a tree is the depth of the deepest node
• A binary tree of depth n is balanced if all the nodes at depths 0 through n-2
have two children
n-2
n-1
n
12 12 12
8 3 8 12 8 14
Blue node has Blue node has Blue node does not have
heap property heap property heap property
12 14
8 14 8 12
Blue node does not have Blue node has
heap property heap property
8 8 10 10
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
Other children are not affected
12 12 14
10 5 14 5 12 5
8 14 8 10 8 10
• The node containing 8 is not affected because its parent gets larger, not smaller
• The node containing 5 is not affected because its parent gets larger, not smaller
• The node containing 8 is still not affected because, although its parent got smaller, its
parent is still greater than it was originally
A sample heap
• Here’s a sample binary tree after it has been heapified
25
22 17
19 22 14 15
18 14 21 3 9 11
11
22 17
19 22 14 15
18 14 21 3 9 11
• How can we fix the binary tree so it is once again balanced and left-justified?
• Solution: remove the rightmost leaf at the deepest level and use it for the new root
The reHeap method I
• Our tree is balanced and left-justified, but no longer a heap
• However, only the root lacks the heap property
11
22 17
19 22 14 15
18 14 21 3 9
22
11 17
19 22 14 15
18 14 21 3 9
22 17
19 11 14 15
18 14 21 3 9
22
22 17
19 21 14 15
18 14 11 3 9
25
22 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
Removing and replacing the root
• The “root” is the first element in the array
• The “rightmost node at the deepest level” is the last element
• Swap them...
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
• ...And pretend that the last element in the array no longer exists—that is, the “last index”
is 11 (9)
Reheap and repeat
• Reheap the root node (index 0, containing 11)...
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25