Length i systemoutprintlnarri operationsperformedonan

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ry
management)
 …
 John Edgar 7 !  !  Instead
of
timing
an
algorithm,
count
the
number
of
 instructions
that
it
performs
 The
number
of
instructions
performed
may
vary
 based
on
 !  The
size
of
the
input
 !  The
organization
of
the
input
 !  The
number
of
instructions
can
be
written
as
a
cost
 function
on
the
input
size

 John Edgar 8 Java public void printArray(int arr){ for (int i = 0; i < arr.length; ++i){ System.out.println(arr[i]); } } Operations
performed
on
an 
array
of
length
10
 |
 |||
|||
|||
|||
||| ||| ||| ||| ||| ||| |
 perform
comparison, 
print
array
element,
and 
increment
i:10
times 
 make 
comparison 
when
i
=
10 
 declare
and 
initialize
i
 John Edgar 9 !  !  !  Instead
of
choosing
a
particular
input
size
we
will
 express
a
cost
function
for
input
of
size
n
 Assume
that
the
running
time,
t,
of
an
algorithm
is
 proportional
to
the
number
of
operations
 Express
t
as
a
function
of
n
 !  Where
t
is
the
time
required
to
process
the
data
using
 some
algorithm
A
 !  Denote
a
cost
function
as
tA(n)
 ▪  i.e.
the
running
time
of
algorithm
A,
with
input
size
n
 John Edgar 10 public void printArray(int arr){ for (int i = 0; i < arr.length; ++i){ System.out.println(arr[i]); } } Operations
performed
on
an 
array
of
length
n
 1
 3n
 perform
comparison, 
print
array
element,
and 
increment
i:
n
times 
 1
 make 
comparison 
when
i
=
n 
 declare
and 
initialize
i
 t
=
3n
+
2

 John Edgar 11 !  The
number
of
operations
usually
varies
based
on
 the
size
of
the
input
 !  Though
not
always,
consider
array
lookup
 !  In
addition
algorithm
performance
may
vary
based
 on
the
organization
of
the
input
 !  For
example
consider
searching
a
large
array
 !  If
the
target
is
the
first
item
in
the
array
the
search
will
be
 very
quick
 John Edgar 12 !  Algorithm
efficiency
is
often
calculated
for
three
 broad
cases
of
input
 !  Best
case
 !  Average
(or
“usual”)
case
 !  Worst
case
 !  This
analysis
considers
how
performance
varies
 for
different
inputs
of
the
same
size
 John Edgar 13 !  It
can
be
difficult
to
determine
the
exact
number
of
 operations
performed
by
an
algorithm
 An
alternative
to
counting...
View Full Document

This note was uploaded on 04/17/2010 for the course CMPT 11151 taught by Professor Gregorymori during the Spring '10 term at Simon Fraser.

Ask a homework question - tutors are online