### Comp 271-400 Week 3

Lewis Tower 410, 4:15-8:15

Welcome

**Readings**:

Bailey chapter 6, on sorting

Bailey chapter 9, on lists (and, in particular, linked
lists)

Bailey chapter 15.4.2: (chained) hashing

Morin chapter 1, sections 1.1 and 1.2

One slight peculiarity of Morin is
that he refers to the array-based List implementation of chapter 2 as an
Array*Stack*.

Primary text: Bailey, online, and maybe Morin, also online.

Information about MSDNAA is in the Intro to C++
section

### Linked List

See lists.html#linked.

See also linkedlistij.zip.

Class both.java in linkedlistij.zip: change from ArrayList to LinkedList.

#### List-related examples:

####

#### Table of Factors

This is the example on Bailey page 88. Let us construct a table of all the
k<=n and a list of all the factors (prime or not) of k, and ask how much
space is needed. This turns out to
be n log n. The running time to construct the table varies with how clever
the algorithm is, it can be O(n^{2}) [check all i<k for
divisibility], O(n^{3/2}) [check all i<sqrt(k)], or O(n log n)
[Sieve of Eratosthenes].

#### Space in a string

The answer depends on whether we're concerned with the worst case or the
average case (we are almost never interested in the best case). If the
average case, then the answer typically depends on the probability
distribution of the data.

#### More complexity

A function is said to be polynomial
if it is O(n^{k}) for some fixed k; quadratic growth is a special
case.

So far we've been looking mainly at running
time. We can also consider space needs.

### Hashing

Hashing: lists.html#hashing

in-class lab: GetHashCode() values of strings from hashij.zip's
classes hashCodes.java and hashStats.java.

### Chapter 6: Sorting

Quicksort implementations

See sorting.html#sorting

### Recursion

Recursion starts at Bailey page 94

See recursion.html
and recursion1ij.zip.

- Factorial (done)
- Fibonacci (mostly done)
- Induction
- Postage (currently first-class is 49¢, and post cards are 34¢)
- Expressions.

**in-class lab 2**

Install expressionsij.zip.

#### Ratio Revisited

The gcd() method on Bailey page 9 is recursive. How does this work?

In analyzing a recursive method, there are two specific issues: does the
method, when it returns, return the correct value? And does the method ever
return? Then there's the issue of how recursion can work at all. Internally,
the runtime system handles this by creating a separate set of local
variables for each call to gcd(). This is done on the so-called *runtime
stack*. This means that different calls to gcd(), with different
parameter values, don't interact or interfere.

For the return value, note that gcd(a,b) = gcd(a,b%a), always; any divisor
of a and b is a divisor of b%a (which has the form b-ka), and any divisor of
a and b%a is a divisor of b.

Finally, there's the question of whether rgcd() ever *returns*. One
way to prove this is to argue that the first parameter to rgcd() keeps
getting smaller. We stop when it reaches 0, as it must (why can't rgcd()
terminate with first parameter > 0?). The **atomic case**
in the recursion is the case that involves no further recursive calls; in
the gcd() example it is the case when a==0.

Trees

binary trees

insertion and search

traversal

tree-based dictionaries