Professional Documents
Culture Documents
Chapter 4
Data Structures and Abstractions with Java, 4e
Frank Carrano
2015 Pearson Education, Inc., Upper
Saddle River, NJ. All rights reserved.
Class
Business
Today
Eciency
and
Stacks
Intro
Your
work
OpenDSA
HW
and
Pre-Lab
Project
2
Clicker
Poll
Which
Chapter
are
you
most
interested
in
reviewing?
a. Ch
1
Bags
b. Ch
2
Array
ImplementaMon
of
Bags
c. Ch
3
Linked
ImplementaMon
of
Bags
d. Ch
4
Eciency
e. Ch
5
Stacks
2015 Pearson Education, Inc., Upper
Saddle River, NJ. All rights reserved.
Example
Consider
the
problem
of
summing
FIGURE
4-1
Three
algorithms
for
compuMng
the
sum
1
+
2
+
.
.
.
+
n
for
an
integer
n
>
0
2015 Pearson Education, Inc., Upper
Saddle River, NJ. All rights reserved.
Example
What is best?
An algorithm has both time and space
constraints that is complexity
Time complexity
Space complexity
FIGURE
4-2
The
number
of
basic
operaMons
required
by
the
algorithms
in
Figure
4-1
2015 Pearson Education, Inc., Upper
Saddle River, NJ. All rights reserved.
Best, Worst,
and Average Cases
For some algorithms, execution time
depends only on size of data set
Other algorithms depend on the nature
of the data itself
Here we seek to know best case, worst
case, average case
Big Oh Notation
A function f(n) is of order at most g(n)
That is, f(n) is O(g(n))if
A positive real number c and positive
integer N exist
Such that f(n) c x g(n) for all n N
That is, c x g(n) is an upper bound on f(n)
when n is sufficiently large
2015 Pearson Education, Inc., Upper
Saddle River, NJ. All rights reserved.
Picturing Efficiency
Picturing Efficiency
Picturing Efficiency
Picturing Efficiency
Picturing Efficiency
Efficiency of
Implementations of ADT Bag
Reading
Quiz
3
2.
You
should
express
the
complexity
of
an
algorithm
in
terms
of
it
a.
b.
c.
d.
space
requirements
execuMon
Mme
problem
size
overall
complexity
Reading
Quiz
3
5.
If
an
algorithm
requires
7
basic
operaMons
for
an
algorithm
with
a
problem
size
of
n,
the
algorithmic
complexity
is
a.
b.
c.
d.
O(1)
O(7)
O(n)
O(7n)
2015 Pearson Education, Inc., Upper
Saddle River, NJ. All rights reserved.
End
Chapter 4
Lecture 06 07 on Stacks