Analysis of algorithms · Exercises
Fall, 2010 · Department of Computer Science · Grinnell College

Exercise set 2

2.1. Design an algorithm that takes two arguments, a vector v of real numbers and a positive integer n less than the length of v, and returns a freshly allocated vector of n real numbers containing the n smallest elements of v, in ascending order. Implement this algorithm in C and test it. Submit the source code and supporting files by e-mail to stone@cs.grinnell.edu with the subject line [CSC 302] Exercise 2.1.

2.2. Design an algorithm that takes a vector of real numbers as an argument and returns the median of the pairwise sums of its elements. If the number of pairwise sums is even, the median should be the mean of the two middlemost sums. So, for instance, if the elements are 3, 12, 7, and -6, the pairwise sums are 15, 10, -3, 19, 6, and 1 and the median is (6 + 10)/2, or 8. Implement this algorithm in your favorite programming language and test it. Submit the source code and supporting files by e-mail to stone@cs.grinnell.edu with the subject line [CSC 302] Exercise 2.2.

2.3 Determine the asymptotic complexity of the algorithm you designed for exercise 2.2. Submit a file containing your determination by e-mail, attached to a message with the subject line "[CSC 301] Exercise 2.3".

This exercise set will be due on Friday, September 24.

Exercise set 1

1.1. In your favorite programming language, write and test a function that takes any positive integer as argument and returns its iterated logarithm (as defined on page 58 of the textboook).

To submit the source code for your function and any supporting files that you'd like me to see or use in evaluating your implementation, such as the output from your test runs, send an e-mail to stone@cs.grinnell.edu with the subject line "[CSC 302] Exercise 1.1" and attach your file(s) to it.

1.2. Disprove the assertion that n lg n =  Θ(n2). Submit a file containing your disproof, expressed in English extended with mathematical notation, by sending me an e-mail with the subject line "[CSC 302] Exercise 1.2" and attaching your file to it.

1.3. An inversion in a one-dimensional array of real numbers is an unordered pair {i, j} of indices into that array such that i < j and the element at position i is greater than the element at position j. If the elements of an array have been sorted into ascending order, there are no inversions in the array.

Design a recursive divide-and-conquer algorithm that takes a one-dimensional array of real numbers as argument and returns the number of inversions in that array. Implement your algorithm as a function in your favorite programming language and test that function. You may choose whatever data representation you like for one-dimensional arrays, provided that your function works on one-dimensional arrays of any length, including 0. Submit the source code and any supporting files by e-mail, as in exercise 1.1 above, but with the subject line "[CSC 301] Exercise 1.3".

1.4. Give a recurrence that describes, using asymptotic notation, the running time of the algorithm that you devised in exercise 1.3. Using the master theorem, determine asymptotic lower and upper bounds for the running-time function for your algorithm. As above, submit a file containing your determination by e-mail, attached to a message with the subject line "[CSC 301] Exercise 1.4".

This exercise set will be due on Monday, September 13.

· ·
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States License.

This text is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/algorithms/exercises.html


John David Stone · stone@cs.grinnell.edu