CSCI 3104 - Algorithms - Summer 2015

Problem Set #1

Due: Wed, Jun 10th



1. In each of the following situations, indicate whether f = O(g), or f = Ω(g), or both (in which case f = Θ(g)).
(a) f(n) = n1.01, g(n) = n
(b) f(n) = lg n, g(n) = ln n
(c) f(n) = 2n, g(n) = 3n

2. The text spends a lot of time talking about fib(200). Compute this number exactly using Python. Submit your code listing along with the value of fib(200), on paper.

3. Consider the following Python function:

def find(a, target):
    x = 0
    y = len(a)
    while x < y:
        m = (x+y)/2
        if a[m] < target:
            x = m+1
        elif a[m] > target: 
            y = m
        else:
            return m
    return -1
Suppose list a has n elements and is sorted. Using Θ() notation, what is the best case running time as function of n? Using Θ() notation, what is the worst case running time as function of n?

4. Modify the find function from problem 4 by changing the 5th line from m = (x+y)/2 to m = (2*x+y)/3; now answer the same questions given in problem 3.

5. Consider this sorting algorithm, written in Python:

def bsort(a):
    swapped = True

    while swapped:
        swapped = False
        for i in range(0, len(a)-1):
            if a[i] > a[i+1]:
                a[i], a[i+1] = a[i+1], a[i]
                swapped = True
(a) Is this sort randomized or deterministic?

(b) What is the value of the best-case input?

(c) Using Θ notation, what is the best-case running time of this program?

(d) What is the value of the worst-case input?

(e) Using Θ notation, what is the worst-case running time of this program?

6. Consider this sorting algorithm, written in Python:

import random

def luckysort(a):
    sorted = False

    while not sorted:
        random.shuffle(a)
	sorted = True
        for i in range(0, len(a)-1):
            if a[i] > a[i+1]:
                sorted = False
(a) Is this sort randomized or deterministic?

(b) What is the value of the best-case input?

(c) Using Θ notation, what is the best-case running time of this program?

(d) What is the value of the worst-case input?

(e) Using Θ notation, what is the worst-case running running time of this program?

(f) Using Θ notation, what is the expected running time of this program?

7. Text problem 2.4. Justify your answer.

8. Text problem 2.12. Justify your answer.

9. Text problem 2.17. Justify your answer.

10. Let A and B be arrays of integers. Each array contains n elements, and each array is in sorted order (ascending). A and B do not share any elements in common. Give a O(lg n)-time algorithm which finds the median of A union B. In other words, find the median of the 2n elements given by putting A and B together into one array. (Note: Remember the definition from our book (section 2.4) for the median of a list with an even number of elements.)

11. The text doesn't implement their algorithm for Selection. Please do so. Use the first version we did in lecture that recurses even for "bad" pivots. Then, compute the median of the list of words given here using your algorithm. (Note: Use the standard comparison for strings (just like strcmp() in C). All words in this list are distinct.)