Newt Gingrich is given the job of partitioning 2n players into two teams of n players each. Each player has a numerical rating that measures how good he/she is at the game. Newt seeks to divide the players as unfairly as possible, so as to create the biggest possible talent imbalance between team A and team B. Show how Newt can do the job in time.
Take as input a sequence of 2n real numbers. Design an algorithm that partitions the numbers into n pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus the third partition has 10 as its maximum sum, which is the minimum over the three partitions.
Assume that we are given as input n pairs of items, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further, assume that the items are sorted by number. Give an O(n) algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted.
For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow).
Given two sets and (each of size n), and a number x, describe an algorithm for finding whether there exists a pair of elements, one from and one from , that add up to x. (For partial credit, give a algorithm for this problem.)
For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, , 19-3 maximizes the difference, while 8-6 minimizes the difference.
(a) Let S be an unsorted array of n integers. Give an algorithm that finds the pair that maximizes |x-y|. Your algorithm must run in O(n) worst-case time.
(b) Let S be a sorted array of n integers. Give an algorithm that finds the pair that maximizes |x-y|. Your algorithm must run in O(1) worst-case time.
(c) Let S be an unsorted array of n integers. Give an algorithm that finds the pair that minimizes |x-y|, for . Your algorithm must run in worst-case time.
(d) Let S be a sorted array of n integers. Give an algorithm that finds the pair that minimizes |x-y|, for . Your algorithm must run in O(n) worst-case time.
Use the partitioning step of Quicksort to give an algorithm that finds the median element of an array of n integers in expected O(n) time.
The running time for Quicksort depends upon both the data being sorted and the partition rule used to select the pivot. Although Quicksort is on average, certain partition rules cause Quicksort to take time if the array is already sorted.
(a) Suppose we always pick the pivot element to be the key from the last position of the subarray. On a sorted array, does Quicksort now take , , or ?
(b) Suppose we always pick the pivot element to be the key from the middle position of the subarray. On a sorted array, does Quicksort now take , , or ?
(c) Suppose we always pick the pivot element to be the key of the median element of the first three keys of the subarray. (The median of three keys is the middle value, so the median of 5, 3, 8 is five.) On a sorted array, does Quicksort now take , , or ?
(d) Suppose we always pick the pivot element to be the key of the median element of the first, last, and middle elements of the subarray. On a sorted array, does Quicksort now take , , or ?