CSC 103 Lecture Notes Week 6
Program Design Issues;
Analytic and Empirical Running Time Calculations
* Phase 1: Test the constructor, building tables of sizes 1, 5, 500, and * the default size; confirm the size of each table. * * Phase 2: Test the enter method, lookup, and delete methods on table of * size 1. * * Phase 3: Test enter method by filling up a table of size 5, including * check of the TableFull exception. * * Phase 4: Test the lookup method on the full table of size 5, expecting * O(N) performance on lookups given full table. * * Phase 5: Test the delete method on table of size 5, removing each entry. * * Phase 6: Repeat phases 3 through 5 to exercise the somewhat tricky * implementation of delete that uses active/inactive flags. * * Phase 7: Successively test the enter, lookup, and delete methods on the * the same table of size 5, with 0 through 7 entries. Expect * O(N) performance on lookups since entries are all marked as * inactive instead of being null. * * Phase 8: Rerun phase 7 on a new table of size 5, expecting O(c) * performance on early lookups. * * Phase 9: Test enter and lookup on a larger more sparsely populated * table, expecting O(c) performance.
public int findLargest(int[] numbers) {
/* 1 */ int largestValue; // Largest value so far
/* 2 */ int i; // Array index
/* 3 */ if ((numbers == null) || (numbers.length == 0)) {
/* 4 */ return Integer.MIN_VALUE;
/* 5 */ }
/* 6 */ for (largestValue = numbers[0], i = 1; i < numbers.length; i++) {
/* 7 */ if (largestValue < numbers[i]) {
/* 8 */ largestValue = numbers[i];
}
}
/* 9 */ return largestValue;
}
public HashTable enter(HashTableEntry entry)
throws HashTableFull, HashIndexInvalid {
int index; // Hash index
int i; // Vacant entry search loop index
/*
* Time = time of getKey method + time of hash method.
*/
index = entry.hash(entry.getKey(), size);
/*
* Time = 2 constant compares + constant throw + constant constructor.
*/
if ((index < 0) || (index >= size)) {
throw new HashIndexInvalid(index);
}
/*
* Time = two constant array accesses, one compare, two assigns, one
* return.
*/
if (table[index] == null) {
table[index] = lastEntry = entry;
return this;
}
/*
* Time = constant array access + return + 2 * time of getKey + time of
* equals.
*/
if (table[index].getKey().equals(entry.getKey())) {
return this;
}
/*
* Time = K * time for loop body, where
* K is the number of times through the loop
* loop body time = times for elements as computed above
*/
for (i = index + 1; i != index; i++) {
/*
* If we've come to the last entry in the table, set the loop index
* to -1 so the search will continue at the top of the table.
*/
if (i == size) {
i = -1;
continue;
}
/*
* Output some trace information.
*/
System.out.println(" Probing entry " + i +
" with entry key " + entry.getKey());
/*
* If we've come to an empty table spot, put the entry there. Save
* the entry in the lastEntry data field, for use by lookup.
*
*/
if (table[i] == null) {
table[i] = lastEntry = entry;
return this;
}
/*
* If the entry at the ith spot has the same key as the given
* entry, we quit without doing anything.
*/
if (table[i].getKey().equals(entry.getKey())) {
return this;
}
}
/*
* If we've come out of the loop, it means we've fully exhausted all
* table entries, which means the table is full.
*/
throw new HashTableFull();
}