- Administriva
- Quiz Thurs!
- Tutors in lab TWTh ("...even then calendar, after Monday and Tues, says, 'WTF'!"
- The big picture: what's the plan?
- Acquire Java programming skill, expertise in common algorithms and data structures
...by implementing various algorithms using various data structures
- Learn to analyze the running times of those algorithms
...and verify, empirically, that the theoretical analysis are correct.
- A silly (ridiculously slow) sort: random sort for a list of n elements -- what is its running time? how to time it empirically??
while the list is out of order (ooo){
int i = rand(n);
int j = rand(n);
swap the ith and jth values in the list
}
- What's wrong with this way to swap the ith and jth elements of an array?
swap(list[i], list[j]);
...
void swap(int x, int y) {
int pocket = x;
x = y;
y = pocket;
}
- Improved random sort; only swap if OOO! Is it faster? Is it's O running time different?
- A simple (not quite as slow) sort: Bubble sort for a list of n elements
iterate n-1 times {
for the ith element (from first to last-1) {
if (out of order (relative to the next element)
swap ith and ith+1th values, ith and next value?
} // each pair
} // outer loop
- How many operations will it take to bubble sort a list of n elements?
- What is the running time of an algorithm? How to talk about it explicitly.
- Theoretical: An intro to theta notation, by way of big-O notation.
The running time of an algorithm, A, executed on input of length n, is denoted TAn.
A is said to be O(f(n)) if there exist positive constants c, and n0 such that TAn < c * f(n), for all n > n0.
- Empirical: Time the algorithm!
- A difference between theory and practice: can something be trivial theoretically, yet impossible practically?
- You can trade space for time (and vice-versa). E.g. calculating Fibonacci numbers.
- If there is time: displaying Conway's Life graphically.
- If there is even *more* time (as if): reading files (and parsing them).
- A wrapper class for BufferedReader, MyReader
- String[] split()
- one character at a time
- StringTokenizer
- Scanner