













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Professor: Dorr; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;
Typology: Study Guides, Projects, Research
1 / 21
This page cannot be seen from the preview
Don't miss anything!














CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Last time: 1. Overloading and overriding revisited
Interfaces and class inheritance
Interface hierarchies Today: 1. Project #8 assigned!
Course Reviews
Linear search
Binary search
Time complexity analysis
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Read entire assignment from beginning to endbefore starting to code
Check out assignment now from CVS
Follow the instructions exactly , as much of grading is automated
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
A basic operation in computer science
Problem formulation z Given: z
Find: a value having the property in the array
Examples z Find a particular element
Find the least element
We will study different algorithms for solving thisproblem
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
This is a search problem
The collection: numbers 0 … 1,
The “property”: number chosen by the other party
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Answer is “yes / higher / lower”
If “higher”, adjust lower bound
If “lower”, adjust higher bound
How long does it take?
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Linear search?
Binary search?
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Number chosen may be 1,
This would require each number to be guessed
Each guess rules out half of the remainingpossibilities
Total number of possibilities: 1,
This can be cut in half at most 10 times
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
2
log 2 n = number of times n can be cut in half
log 10 n = number of times n can be divided by 10
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) Binary Search: As data size n grows, doubling the data yields runtimes that increment by a constant amount z
b^
z
n → ∞
z
First, note that T(n) = a log b^ cn + d and T(2n) = a log b^ c(2n) + d z Next, examine T(2n) more closely: z T(2n) = a log b^ c(2n) + d z = a log b^ 2(cn) + d z = a[log b^ 2 + log b^ (cn)] + d z = a log b^ 2 + a log b^ (cn) + d z = a log b^ 2 + [ a log b^ (cn) + d ] z = a log b^ 2 + T(n) z Since a log b^ 2 is simply a constant, we can set k = a log b^ 2 z So lim n→ ∞ T(2n) = T(n) + k, where k is a constant √ z
z
2
n T(n) 64 6 128 7 256 8 n log 2 n Increments by 1 Increments by 1 Increments by 1
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
We say: z “Linear Search is θ (n)” z “Binary Search is θ( log 2 n) z θ(
θ( log (log n)) θ( log n) θ( log n * log n) θ( n) θ( n log n) θ( n log (log n)) θ( n logn * logn) θ( n
) θ( n 2 ) θ( n 100000000 ) θ( 2 n ) θ( 3 n ) θ(
n ) θ( 4 n ) θ( n!) θ( n n ) Huge chasm betweenpolynomial and exponential
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Asymptotic = “as n gets large” z Worst-case = “how many guesses in the worst case?” z Time complexity = “how much ‘time’ is consumed?” z Giving accurate “to-the-millisecond” characterizations notpossible because algorithms can run on different computers z Time complexity instead refers to “number of basic operations” z Basic operation for search game = guess z If n is a number, then f(n) is a function on that number (e.g. f(n) = 3n). z If an algorithm has worst-case processing time of f(n) for inputsize is n, then it is said to have asymptotic worst-case timecomplexity O(f(n)) (“big-oh of f(n)”) z Linear search is O(n) z Binary search is O(log 2 n)
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
public static int linSearch (String[] a, String elt) { for (int i = 0; i < a.length; i++) if (elt.equals(a[i])) return i; return -1; // return
if elt is not in a } z Analysis based on coarse assumptions z Comparisons are “one operation” z Assignments are one operation z What about linSearch ? z Loop executes n times, where n is number of elements in a z Each loop iteration: z Comparison involving i z Comparison involving elt ,^ a[i] z One assignment to i z So three operations per loop iteration z So worst-case time complexity is: O(3n) z By convention, constants omitted, so O(n)