Comp 271-400 Week 4

Crown 103, 4:15-8:15




    Bailey chapter 12 on binary trees, especially sections 3, 4 and 6
    Bailey chapter 14 on binary search trees, sections 1-4

    Morin chapter 6 on binary trees, sections 1 and 2

Exam next week

It will probably be online, on sakai. Stay tuned!

The study guide is on Sakai. Answers will be posted at the end of this week.

You can bring up to three pages (sides) of your own printed or handwritten notes. You will also receive a copy of, which includes examples of basic Java constructions.

There's an algorithmic reason why quicksort's speed is affected by whether the data is already sorted: if we pick data[left] as the pivot in a sorted array, we'll partition into an empty left half and a right half of size N-1.

But why should mergesort be faster if the data is already sorted? The algorithm executes exactly the same steps!
Demo: pldsorting/, prev/sortspeed

Related question: why is insertion sort faster than selection sort?

How fast is quicksort? If you divide an interval in half, at random, what is the expected value of the smaller and larger parts?

Radix Sort

    133    312    121    213    122    211    321    132   223    332

Naive approach: bucketize on the first digit, then sort the buckets recursively.

1337 approach: bucketize on the last digit, concatenate, bucketize on second-to-last digit, concatenate, bucketize on the first digit (third-to-last digit)


binary trees
binary search trees
insertion and search
tree-based dictionaries


Recursion starts at Bailey page 94
See recursion.html and

in-class lab 1

Install and implement exponentiation

New grammar:

    /* Grammar:
    expr   ::= term { addop term }
    term   ::= efactor { mulop efactor }
    efactor ::= factor [ '^' factor ]
    factor ::= number | '(' expr ')'
    /* */

Changes: modify termeval() so as to call efactorEval(); add efactorEval():

    result = factoreval();
    if (t.equals(theToken, "^") {
            theToken = t.token();
            long res2 = factorEval();
            result = power(result, res2);

    private long power(long num, long exp) {    // this is actually C#. Can you tell?
        long prod = 1;
        for (int i=0; i<exp; i++) prod *= num;
        return prod;