Allan Weiss
Allan Weiss
Allan Weiss
142, 543, 123, 65, 453, 879, 572, 434, 111, 242, 811, 102.
Solution.
Step 1 Build the heap.
1.1 Place all the data into a complete binary tree in the order given. You should
get:
142
543 123
1.2 Now turn it in to a maximum heap (i.e. that is the root is the largest element
of each subtree). You should get:
879
811 572
811
543 572
879
After the fifth deletion the heap should look like this:
434
242 142
3 1 41 5 5 2 6 5 3 9
The first, middle, and last element are sorted
3 1 41 5 3 2 6 5 5 9
The median is "hidden" in the second last position.
3 1 41 5 3 2 5 5 6 9
Partition the remainder of the elements and swap the pivot back.
There are only two elements in the RHS partition. An insertion sort subrou-
tine is called to sort these two elements. Quicksort is recursively applied to the
elements in the LHS of the pivot.
1st 4th 8th
3 1 4 1 5 3 2 5
Initial Set
1 1 4 3 5 3 2 5
The first, middle, and last element are sorted
1 1 4 2 5 3 3 5
The median is "hidden" in the second last position.
1 1 3 2 3 4 5 5
Partition the remainder of the elements and swap the pivot back.
Again the number of elements to the RHS of the pivot is ≤ 3 and thus are
sorted with a call to an insertion sort subroutine.
1 1 3 2
The first, middle, and last element are sorted
1 3 1 2
The median is "hidden" in the second last position.
1 1 3 2
Partition the remainder of the elements and swap the pivot back.
Now the number of elements of both the LHS and the RHS is less than equal
to the cut-off of 3 and so is sorted using an insertion sort subroutine.
Question 7.20 Using the quicksort implementation of this chapter, determine
the running time of quicksort for:
Solution.
(a) O(N log N ) . This is because the size of the problem is exactly halved at
each step, at a cost that is linear to the current size.
(b) O(N log N ) . For the same reasons as in (a).
(c) This question requires you to do an average case analysis of quicksort. This
is done in the notes. The answer is O(N log N ).
Question 7.32 Suppose you are given a sorted list of n elements followed by
f (n) randomly ordered elements. How would you sort the entire list if
1. f (n) = O(1)
2. f (n) = O(log
√ n)
3. f (n) = O( n)
4. How large can f (n) be for the entire list to be still sortable in O(n) time?
Solution. Looking at each case:
1. If there are just a handful of unsorted elements – a constant number, say, k
– at the end then we could just run k iterations of the inner loop of insertion
sort. This will be a running time of O(k · n). Alternatively, we could sort
these k elements separately and then perform a merge: a constant number
of elements can be sorted in a constant amount of time and the merge takes
n + k writes into a new array and another n + k to copy back to the original.
2. If there are O(log n) extras then the insertion-sort approach will require
O(n log n) time. If we independently sort them and then merge the two
pieces together we will take time: O(log n·log log n) to sort the extra elements
and then, as in the previous part, merge the two pieces in time n + log n
(merge/write)√and n + log n to copy back, which is in√all O(n). √ √
3. If there are
√ O( N ) extras then we can sort these in O( n log n) = O( n log n)
since log n = log n − log 2 = O(log n). Note that this sorting is still o(n)
(dominated by a multiple of n). √
Merging can be done in two steps of, respectively, n + n (merges/writes)
and the same to copy back. This is still O(n).
4. If f (n) = O(n/ log n) then sorting can be done in
n n n
O( log ) = O( (log n − log log n)) = O(n) − o(n)
log n log n log n
Question 7.32 Prove that any algorithm that finds an element x in a sorted
list of n elements requires Ω(log n) comparisons.
Solution. This can be argued by the “20 questions”-style of argument that we
used for lower bounds on sorting. Since we could imagine an input that would
require us reporting any of the n different array positions we have to have a
separate outcome (leaf node) corresponding to each of these possibilities.
In order for a binary tree (since only 2-way comparisons are allowed) to have
n leaves we require a depth of at least dlog ne = Ω(log n).
N √
Question 7.34 Using Stirling’s Formula, N ! ≈ Ne 2πN , give a precise
estimation for log(N !).
Solution.
N
N √
log(N !) ≈ log 2πN
e
N
N √
≈ log + log 2πN
e
√
N
N
≈ log N
+ log 2πN
e
√
≈ log N − log eN + log 2πN
N
≈ log N N − log eN
≈ N log N − N log e
Question 2. Row (column) sorting an array of numbers means taking each row
(column) and sorting numbers in that row. Given an m × n array of numbers
(m rows of n numbers), if the array is row-sorted and then column-sorted, is the
array still row-sorted?