Homework #2

Mostly searching, sorting

# problems
1 GT Exercise R-3.3 on page 110
Suppose you have a binary search tree, T, storing numbers in the range from 1 to 500, and you do a search for the integer 250. Which of the following sequences are possible sequences of numbers that were encountered in this search.

For the ones that are possible, draw the search path, and, for the ones that are impossible, say why.

a. (2, 276, 264, 270, 250)
b. (100, 285, 156, 203, 275, 250)
c. (475, 360, 248, 249, 251, 250)
d. (450, 262, 248, 249, 270, 250)

2 GT Exercise C-8.5 on page 260
Suppose we are given a sequence S of n elements, each of which is colored red or blue. Assuming S is represented as an array, give an in-place method for ordering S so that all the blue elements are listed before all the red elements. Can you extend your approach to three colors?
3 CLRS Exercise 8.1-4 on page 194
Suppose that you are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence. Thus, all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences. Show an Ω(n lg k) lower bound on the number of comparisons needed to solve this variant of the sorting algorithm. (Hint: It is not rigorous to simply combine the lower bounds for the individual subsequences.)
4 GT Exercise C-8.3 on page 259
Suppose we are given two n-element sorted sequences A and B that should not be viewed as sets (that is, A and B may contain duplicate entries). Describe an O(n)-time method for computing a sequence representing the set AB (with no duplicates).
5 Baase Exercise 5.3 on page 242
We wish to establish a lower bound for finding the minimum and maximum of n keys. What lower bound can we get from a decision tree argument?

# hard problems
6 Baase Exercise 1.47 on page 66
The first n cells of the array E contain integers sorted in increasing order. The remaining cells all contain some very large integer that we may think of as infinity (call it maxint). The array may be arbitrarily large (you may think of it as infinte), and you don't know n. Give an algorithm to find the position of a given integer x (x < maxint) in the array in O(log n) time.
7 CLRS Exercise 8.1-3 on page 194
Show that there is no comparison sort whose running time is linear for at least half of the n! inputs of length n. What about a fraction of 1/n of the inputs of length n? What about a fraction of 1/2^n?
8 Let A be an array of size n of integers, where A[1] < A[2] < ... < A[n].
(Note that each entry may be a positive or negative integer.)

(a) Give an algorithm that takes O(log n) time to find an i such that A[i] = i, provided such an i exists. If no such i exists, the algorithm returns 0.

(b) Prove that any algorithm to solve this problem using comparisons must take time Ω(log n).


Last modified: Jun 11, 2019