Fnsttions: Algorth
Fnsttions: Algorth
Charactey (sttes ;
algortthm,
Finateness : f t ace ut of fnstnction 0f. an
s
Ahoithm Sum(A,n)
S-I
S=StAti] –n
A-n
Yeturn S,
20
n
for lzo; íen; Îtt)
(
n
for (je0;jen,itt) nxn)
K-1
tor(Kso Ken; Ktt)nnintl) hT
3n+u
for(izo, j<nsitt)
Stmt;
for (i=0, i<n; itt)
tmt, stmt;
P kk4)
Ke Jn
Stnt;
Jtmt;
for (fe o, trien,tt)
stmt;
J
forCjeo;jenijtt)
Stmt
Stmts
while (rnn)
12
else
=p-n,
Algo testh
(nes)
pirtf
(orditon intutru
(vd', n) -
else 2+2
Condtintalse in
-Ayeise
tsutd
I<logn<sh<n<nlogn<ntz...<
(îrementa) ordea ot ttme complesity
tfme
n
10qn
2.
2
3 64 a56
8| 5|2
05loql24
ASmptotic Notaions!
Asqmptotie Notaions are used to shouw the clas, of a function.
we have 3 NOtalioms
Big ob
)Theta
+ 1f we
fnda
TÍme function of anyalgoithm then
it to
be among any one of these or muHtple of those.
If t (s m0t a multfple of those theH we mnay notbe
able to Specify the Theta motation, we use
the
Omega notatton.
Biq oh notaion:
The functon f ch) =
Oqn ff3 tve onstant. cgn
Such ttat
need to chouse mearest
2n 19
2n+3 ( Upper bound
20+3< 2nt3)
O
Omega totation ():
The
funton fen) =ngn 1bb tv2 constant ce
2n+3>n
Theta
Jn<2nt3
fo=2t3nt4
Biy oh
Otrty
Theta:
<
in 2nt3nty e
2ntn
neilogn)
Reflexive propety
t
fCn ls gtven then fn)
=0 (4c) 0tA3
Transttive propety:
Transpose Symnetic
(qem) hen
O
qn=un (-fen
urth
Samples' loqn (toqn°)
wth
loq ialogn< Blogn
f( Small ())
else
Divtde P in to p,, PK S
PACCP)
coTndne (DACCPD,... -
Dht (P)
Test(n) ;
Tyee mthod:
TC3) "(n) Vod Test (fotn
TC2)
nt 2
Tco)
test(o-1);
Ten
TCnt0tI
= TCn-2)+)
Substttution method:
T UD)= TCN-Ut|
=(T(n-29t)t|
Ttn-2)t2
lT(3)+1J+2 TCn)T(n-ntn
TCn-8)+3
k term
Ten
eee
T(ote)K On
void Test (Ent n
-
2
TO)=1(n-1)+nTeUsíon n
Substttt or method:
=
T(n-))tn-tn
Kterms
T
T(O-n) +( n-nt) + (o-nt2)t
nnt)
vold Test ( int n)TS)
Test
(n-)} -1(0-1)
Tyee ethod;
--lon
Ten)
logina) T(o-3)
1(2)
105
1lo)
ogt og
tn19tlo (n-)+ logln3)
ogtloqt)+ toq to)
nlogn
TO) = T(O
tlogb o(nogn)
TM Tn-2) 0tn)
T0)T(n-50)+n
0cn
Alyutbm:
Tiee melhad:
Test(nt n)
t(0) 21(n-1)+ 2
-21(n-)+1
1t242+
Sum of Gip tevms KH
2
ovem for decveasnq funtions
Masters the
TCn= aTCn-b)t ftns
0(n*fcon)
3) 16 as10 (nab
0(ftos ab)
Alqothm:
Test (n
Pt ("%d n);-1
Test (na) T(DI;) 7 )
} 1(=TOh) t2
=1(n})+1
Ten+n=
hejbolo
|<4
ASL
(i)l.-(lu)
(-lu)L
cuboru)o
Mastes heovtm for dlvidna funcfons.
TO) =
aT(n/b) +ftn
ttere we
derfve tuwo
terms
* K
K=l
tog
loq= = 2
Ke |
2 >|
CAse : 1f loq a k then
* it p>-tthen (roq")
iogh
a=2 b=
Kel P:o ar2, b2 Ke Lp*-1
Vb og,nlo5e
P
K
loqg- loq
Joga
P+!
Pz then (nloglogp)
o(ntoqogn)
(3 Tn)=T(n2)t
a2, b
2, Kel,P:-2
Doq- togi-l
Log 0 <2
Alqoithm:
Testco Tin)
S
trnt, |
Test(sn)) T(Un)
T(2.1(n)K
7
!
Binary &eavcb
62
3
214 5 29 3||36 2 453/b5
13 1y 15
2 3 S 8
Key|2!
mid
12
15 ItIS
( Key<nid)
( 5)
Key= 80:
Yatd=
th 2 eey mid
15 It15 (30>q)
5 9415
2 (36 <4)
he mid
2 (30< 36)
qtgq (30< 31)
5
element not founc.
Key =y2:
rmid= Lth
15
t)
igonithro!
Binanydanch (A,n,Key)
while c5)
mtdr lth
H(Eeyz A[mid))
Yetun mid;
fCKey< Atmid])
hemd-1,
Key A CmtdJ)
l= midt
retun o
Recunsive!
RBfnseanch ( L,h,Key)
10)
ef(a==b)
eturn l;
e lse
return 0;
else
mtdz ( th/2
=
4(Key> A
[mid]) -1
retusn mid;
t+ (Key<A[mid)
setusn RBinscench(1, mid-l Key
T()
else
return RBnSearcb(mkdtl, b,key; Tn,)
Yeturn o)
Tloql241
uick sort:
2
12 15 3 16
6
I0>3
I015
16
5 105
Partsion (e,h T
Alqonttbm ufckSort (h
pfvot AJ
Í--;
}uhile (A3J>pivot);
Suap (Art3,AD;
Swap (A(3, plvot);
o(nlogn
hericseogn 30 loql2y
or
(K-o; K<n;kt)-1xtn
fim-on)
br bin Cn
ba b2)
Alqo n
mm[A,B, -TO
1f (ng2)
formulas
else
mid = ;
mm (ABu,TD2)+ mm (2 B2 D1)
mm (A2) B2 "2)t mm ( A22
B2 D½)
Formulas:
p=AntA1)ButBa2)
Q- (A1t Aa2) (B)
S
A,(BA- B)
T= (Aut Ai2)(Bs)
U= (Aa -An)(B,+64)
V= (A2 -A22) (B.,+ B22)
Cin
PtS-T+V
C)= tstS
C2n P+R-Qte
1
TCN)=
2 b22, Ke 2
b toq
2.81
fc)= 0Cn)