AMP
3/9/2024
Inserion Sort
Data can be in various for mat
{cgpa: Name}
Sotellite ata.
oriaina order
* Stable Soring sattelite data 4g
maintain 24
* Unstable sorting den maintain
(unstable )
0 ’{o, 10, lo,20
1o, 10, 20,
i0:0, t0:A10:8, 20:
A lo:6, 20:c, o:pt - (stabley
11o:
6 1 based indexing
296 13
|A:
key
for i 2 ton
key = A[i1 key2 J=1, AU1 =5
j= i-1
while j>0and ALj] > key:
Ai]: keg
Proof of Correctrness :
Loop invarian
Inihal'z ation ’
useful
e 213 Cpraperty
o
Terminaion ’ Termiate
hat accertouns
correctuess)
Maintepance Termimation
Iniializaton
in every iteration n1-1]= A[1:]
A
[4:4]=4} A[: i-1) is A[I:
sorted is sorted
15 sorted
Unear Search
Analysis
Ruming
TO)
AMP
8/9/2024
Sunday
Best case of Inseron Sort.
Whentae data
daa is already sorted
t t
t Cg
T)
(n-)
G-1)
an +b
T) inear
best Case
Inserton sort UM
Wors Case
reverse ordened
data is
"When the 4
3
n(o+)
2
TO)
2
wgdata
are check Avaroge
Case
4Case Wors
4G73 timepolynomia
quadrat an'+ =
T(n)
ietuoction
Order of Gruwth
" Landau Symbo)
0upper bound
’ Tight bound
Lowen bound
Jo)= 3n4 2n
here: oa) q)-n
bound
Lower
f6)
fe)
f)= 2(g0)
an
(902 function
J) - a, ta,x+ a,xt.:t
Tws
Og-)=2(ac)
.Most reliable notation
AMP
-0Notator (Upperr Bound) Mondat
Biq-0
ond no
J f) there exists positive consts., c
nrs
such hat O<t) < cg) fon all
ofsunctions
his neans a set
na 4n2
n'1
fMembe of
e proper wo the set o
Achually. S) E On)
Simplfied, t ) 0 )
500
Jo)= 4n'+ 100n +
Cn
4n+400n 4 500< no1, C= 604
44
4+ 100 500 < C 4
n
true
alwaystrue hence
so this is
n is this e Oh)? Contraicfion
#| Jo) = n°- 100
o 200 el00
then the
100
n irequolity 0s not
staisffed.
nc+ 100 makes -the
inequatity ftse ene fodola)
L- NorATION ( LoWER BouND)
f): there exists posive constarts
n(g)) = c andn, such tbot
# Jn)= 4n 100n + So0
>cn t
4nt+ 100n+ 500
when
n-oo expression
lo0 500
4+ Volue 4
n
C 4 , no1
9-NoTATION(ioHT BouND)
fon) here exists posihve constats G Cg Do
J
such that o<e6) <s f) < Ga6) n,
and t)e ng) Huen
Theorerm
AMR
1o/B/202y
LITTLE-0 NoTATION Non- Tight Pour)
JJor) there eris!s positive constarts ro
LitHe-o Notation,
than )
J) is asympBohicaly smaller
im
ex
n'+4n Lim
im
n3
NoTATION
LITTLE - W
greater than a
Asymptotical'g
lim f(n) 0
fon) n-yo0
im
n>o g(r)
DIVIDE & CoNQUER ALGoRITHMS
Merge Sort
-Strassen's Matrix Multipica ian
Reursive
MERGE SoRT:
A[p:] into
A[p:r] is divided
so
Divide:
A[P:a) A[qu:]
subartays using Merge Sort
Recursively sort tuo
sorttuo
Conque:
Combire: Apply Merge Logic.
p+p
A: |o2305 42
k23
MeRGE (A, i, 2,4)i
La AlP;)Srted 520
R=ALq+t:nl Sooted
Date: 15/o9/2024
Sunday
MERGE (L, B, A): i=0, j=0, k=p
while i2n
i<n and
ALK) = LL'J 20
20S 2
elseT
20
20
520 10
while i<niL
A[4]= Lli]: |2 5 20
it+j ktt
1 2 5310 20
while j<nei
A[K) =R[jl:
MERGE SoRTA, P, ):
return/
MERGE SoR (A, P.4)
MeRGE SoRT (A4
MER
level K+1
k n
T(G) a) TG) T4)
T) Te)
Gn +) laT
ifn1
Relatton Recurrance firant Sigri
Less
oHherwise
o(1)
SoRT: MERG for
Cormbine
otherise
C) (A)+ aTDo)+
dernent) (single c4)
day Tues
o9/2024
2k
n
T)- Gn+e,n
Cn+ c, n qn
MASTER THEOREM
standad form:
T(%)fon) az1, b>l.
= fis asymptotially
positive
Asymptticall y
MERGE sort RR! Postive
T() 2T2)+ ( a=2
b=2
a
driving
tunction
a
watershed tunction