You are on page 1of 3

Efficiency

BIGIDEA.IT
1. No redundant work
2 . No Duplication
3. Big -0 Notation
4. Comparing Rate of Growth
5. Running time

i.
NoRedundantWorU_ Both same code but
,

② is mud faster
than ① ! !

÷\ interested in this ! ! !

:}
0
*
Opportunistic :
stopÉ we know
number is not prime

ii. NoDup1ication_
→ should not repeat work that has been done before
e. g- Fibonacci
looks more elegant , but many

¥.i:÷÷÷÷¥"¥
duplication :

fib (7)
\

fib / 6) fibls)

fibls)
" "

fib / 4) ¥41 "


fib (3)

fib (4)
"
fib (3)
iii. Big-ONotation_ Crate of growth of a function)

"
compares with input n

e. g. 0 Cn)

↳ removes
any addition / multiplications
↳ takes
onI highest rate of growth

e. g. 0 ( F- +1000N ) '
-
n
=
0 (n " )
↳ compare between
is -
prime
"
slow :O Cn
) fast :O ( Tn)

comparingkateofGrowthfcnlvsg.cn
iv.

→ fcn) > cgcn) for all ns ,


no ,
7 for some constant c

e.g.

f- ( )
"

gcn) -2
"
n or
- -

n -

: once n > 2 ,
fcn) grows faster inde-atey.i.fm grows faster

* sometimes there are looping that make use of n .


Make sure to

see properly !

EXAMPLE G)
-

# Selection fort at i ; iterate j times

at j -1 , iterate j -

l times
each time it iterates once in
at j -2 iterate
the main loop, max will iterate , j -2 times

i
call j decreases , compute sum :
each time we max
,

fewer step
so loop in max takes one
-2
,
(j -

1) =
0-11+2 . .
.
-1 (n 3) + In-2)
-

each time
)
.

=
cn¥nn
O Cri) RAP series

}
=
goes through this list
-

j -1 times
# Fibonacci

we can use wishful thinking to solve it .


! !
=

Let 1¥) be the


running time of fibcn) .
Sime fibcn) calls fibln-17 4 fib Cn-

2) ,

@t=TT Base case : Tcn) = 011) if 112


TCn-4.JO#Tln)LTcn-DtTcnt)
constant running time
We know that Tcn )
-1 >

Ican)
,

any;D →

Ten) I 2 Ten -

1)
T d
-
: Tln) 2 2 Tcn -

1) 2 4 Ten -

2) 28 Ten -

3) 2 . . .
2^-11-11) 20 ( I 2h)

I
012 )
"
i. Tcn) =

* exponential ,
much slower than

iterative version

A- collatz
Run time will be unknown
e-

don't know how


as we many
hoops it will make before it

terminates .

You might also like