*This preview shows
pages
1–2. Sign up to
view the full content.*

2/25/2011
1
CS/ENGRD 2110
Object-Oriented Programming
and Data Structures
Spring 2010
Thorsten Joachims
Lecture 8: Asymptotic
Complexity and
O
What Makes a Good Algorithm?
•
Suppose you have two possible algorithms or
data structures that basically do the same thing;
which is better?
•
Well… what do we mean by better?
–
Faster?
–
Less space?
–
Easier to code?
–
Easier to maintain?
–
Required for homework?
•
How do we measure time and space for an
algorithm?
2
Sample Problem: Searching
static boolean find(int[] a, int item) {
for (int i = 0; i < a.length; i++) {
if (a[i] == item) return true;
}
return false;
}
3
Determine if a
sorted
array of integers contains a given integer
First solution: Linear Search (check each element)
static boolean find(int[] a, int item) {
for (
int x : a
) {
if (x == item) return true;
}
return false;
}
Sample Problem: Searching
static boolean find (int[] a, int item) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
int mid = (low + high)/2;
if (a[mid] < item)
low = mid + 1;
else if (a[mid] > item)
high = mid - 1;
else return true;
}
return false;
}
4
Second solution:
Binary Search
Linear Search vs Binary Search
•
Which one is better?
–
Linear Search is easier to
program
–
But Binary Search is
faster… isn’t it?
•
How do we measure to
show that one is faster
than the other
–
Experiment
–
Proof
•
Which inputs do we use?
Simplifying assumption #1:
Use the
size
of the input
rather than the input itself
For our sample search
problem, the input size is
n+1 where n is the array size
Simplifying assumption #2:
Count
the number of “
basic
steps
” rather than
computing exact times
5
One Basic Step = One Time Unit
•
Basic step:
–
input or output of a scalar
value
–
accessing the value of a scalar
variable, array element, or
field of an object
–
assignment to a variable,
array element, or field of an
object
–
a single arithmetic or logical

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
This is the end of the preview. Sign up
to
access the rest of the document.