





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






































Chris BrooksDepartment of Computer ScienceUniversity of San Francisco
Department of Computer Science — University of San Francisco – p.1/
Basic sorting algorithms all run in
(^2) Θ(n)^ time.
We can do better by sorting sublists and combiningresults.
Department of Computer Science — University of San Francisco – p.2/
Department of Computer Science — University of San Francisco – p.4/
Merge lists into a new list,
Maintain three pointers (indices)
i, j,^ and
n
i^ is index of left hand list j^ is index of right hand list n^ is index of destination list
If^ A[i]^ < A
[j] T [n] = A[i], increment
n^ and^ i
else^ T^ [n] =
A[j], increment
n^ and^ j
Department of Computer Science — University of San Francisco – p.5/
T^ (0) =^ c
1
for some constant
c^1
T^ (1) =^ c
2
for some constant
c^2
T^ (n) =^ nc
2)^ for some constant
c^3
T^ (n)^ =
nc+ 2^3 T^ (n/2) =^ nc+ 2(^3
n/^2 c+ 2^3
T^ (n/4)) = 2nc+ 4^3
T^ (n/4)
Department of Computer Science — University of San Francisco – p.7/
T^ (0) =^ c
1
for some constant
c^1
T^ (1) =^ c
2
for some constant
c^2
T^ (n) =^ nc
2)^ for some constant
c^3
T^ (n)^ =
nc+ 2^3 T^ (n/2) =^ nc+ 2(^3
n/^2 c+ 2^3
T^ (n/4)) = 2nc+ 4^3
T^ (n/4) = 2nc+ 4(^3
n/^4 c+ 2^3
T^ (n/8)) = 3nc+ 8^3
T^ (n/8))
Department of Computer Science — University of San Francisco – p.8/
T^ (0) =^ c
1
for some constant
c^1
T^ (1) =^ c
2
for some constant
c^2
T^ (n) =^ nc
2)^ for some constant
c^3
T^ (n)^ =
nc+ 2^3 T^ (n/2) =^ nc+ 2(^3
n/^2 c+ 2^3
T^ (n/4)) = 2nc+ 4^3
T^ (n/4) = 2nc+ 4(^3
n/^4 c+ 2^3
T^ (n/8)) = 3nc+ 8^3
T^ (n/8)) = 3nc+ 8(^3
n/^8 c+ 2^3
T^ (n/16)) = 4nc+ 16^3
T^ (n/16) = 5nc+ 32^3
T^ (n/32)
Department of Computer Science — University of San Francisco – p.10/
T^ (0) =^ c
1
for some constant
c^1
T^ (1) =^ c
2
for some constant
c^2
T^ (n) =^ nc
2)^ for some constant
c^3
T^ (n)^ =
nc+ 2^3 T^ (n/2) =^ nc+ 2(^3
n/^2 c+ 2^3
T^ (n/4)) = 2nc+ 4^3
T^ (n/4) = 2nc+ 4(^3
n/^4 c+ 2^3
T^ (n/8)) = 3nc+ 8^3
T^ (n/8)) = 3nc+ 8(^3
n/^8 c+ 2^3
T^ (n/16)) = 4nc+ 16^3
T^ (n/16) = 5nc+ 32^3
T^ (n/32) =^ knc+ 2^3
kkT^ (n/^2
Department of Computer Science — University of San Francisco – p.11/
Department of Computer Science — University of San Francisco – p.13/
cn c(n/2)
c*(n/2)
c*(n/4)
c*(n/4)
c*(n/4)
c*(n/4)
cn cn cn cn
...^
...^
...^
... Department of Computer Science — University of San Francisco – p.16/
cn c(n/2)
c*(n/2)
c*(n/4)
c*(n/4)
c*(n/4)
c*(n/4)
cn cn cn cn
lg nleve
... Department of Computer Science — University of San Francisco – p.17/
Merge Sort:^ Divide the list two parts^ No work required – just calculate midpoint^ Recursively sort two parts^ Combine sorted lists into one list^ Some work required – need to merge lists
Department of Computer Science — University of San Francisco – p.19/
Quick Sort:^ Divide the list two parts^ Some work required – Small elements in left sublist,large elements in right sublist^ Recursively sort two parts^ Combine sorted lists into one list^ No work required!
Department of Computer Science — University of San Francisco – p.20/