You are on page 1of 7

1lm 8oughgarden

lnLroducuon
Merge SorL
(seudocode)
ueslgn and Analysls
of AlgorlLhms l
1lm 8oughgarden
Merge SorL: seudocode
-- recurslvely sorL 1
sL
half of Lhe lnpuL array
-- recurslvely sorL 2
nd
half of Lhe lnpuL array
-- merge Lwo sorLed subllsLs lnLo one
[lgnores base cases]
1lm 8oughgarden
C = ouLpuL [lengLh = n]
A = 1
sL
sorLed array [n/2]
8 = 2
nd
sorLed array [n/2]
l = 1
[ = 1
for k = 1 Lo n
lf A(l) < 8([)
C(k) = A(l)
l++
else [8([) < A(l)]
C(k) = 8([)
[++
end
seudocode for Merge:
(lgnores end cases)
1lm 8oughgarden
Merge SorL 8unnlng 1lme?
key Cuesuon : runnlng ume of Merge SorL on array
of n numbers ?
[runnlng ume ~ # of llnes of code execuLed]
1lm 8oughgarden
C = ouLpuL [lengLh = n]
A = 1
sL
sorLed array [n/2]
8 = 2
nd
sorLed array [n/2]
l = 1
[ = 1
for k = 1 Lo n
lf A(l) < 8([)
C(k) = A(l)
l++
else [8([) < A(l)]
C(k) = 8([)
[++
end
seudocode for Merge:
(lgnores end cases)
2 operauons
1lm 8oughgarden
8unnlng 1lme of Merge
upshoL : runnlng ume of Merge on array of
m numbers ls
(Slnce )
1lm 8oughgarden
8unnlng 1lme of Merge SorL
Clalm : Merge SorL requlres
operauons
Lo sorL n numbers.
8ecall : = ls Lhe #
of umes you dlvlde by 2
unul you geL down Lo 1

You might also like