Assignment 2
Assignment 2
Assignment 2
1. Given an array A of n positive integers, the difference of the ordered pair (i, j) where i ≤ j is A[j]−A[i].
2. Given an array A of n integers, the sum of its sub-array is A[i, j] = A[i] + A[i + 1] + ... + A[j].
The textbook algorithm uses a reduction amongst the problems and gives O(n log n) solutions for them.
1. Design and analyze divide and conquer algorithms (without reduction) to solve them in O(n) time.
2. Design and analyze linear sweep algorithms (without reduction) to solve them in O(n) time.
Hint: Scan the Array from left to right and maintain the necessary data structures.
1
5 kth quantiles, multiary Quicksort [20 Marks]
In Quicksort a pivot element partitions an array of numbers to those smaller than the pivot and those larger
than the pivot (assume for simplicity that all numbers are unique). Consider a multiary version of Quicksort,
where k − 1 pivots are chosen to partition the numbers to k classes analogously to the standard Quicksort.
Show that such multiary partitioning can be produced in O(n log k) time so that the all classes are of the
same size (to within 1).