How to Win Coding Competitions: Secrets of Champions
Week 3: Sorting and Search AlgorithmsLecture 1: Introduction to Sorting
Maxim BuzdalovSaint Petersburg 2016
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Wir
th
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Ab
elso
n
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Ker
nig
ha
n
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Kn
uth
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Nor
vig
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Tu
rin
g
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Ah
o
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Go
del
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Bro
ok
s
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Ga
mm
a
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Str
ou
stru
p
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Sp
ols
ky
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Sk
ien
a
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
La
mp
ort
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
raF
oris
ek
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Ru
ssel
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Rit
chie
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Su
ssm
an
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Gra
ha
m
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Rit
chie
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Pa
tash
nik
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Gra
ha
m
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Cor
men
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Pa
tash
nik
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Cor
men
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen on this bookshelf
Wir
th
Ab
elso
n
Ker
nig
ha
n
Kn
uth
Nor
vig
Tu
rin
g
Ah
o
Go
del
Bro
ok
s
Ga
mm
a
Str
ou
stru
p
Sp
ols
ky
Sk
ien
a
La
mp
ort
For
isek
Ru
ssel
Su
ssm
an
Rit
chie
Gra
ha
m
Pa
tash
nik
Set
hi
Ta
nen
ba
um
Eck
el
McC
on
nel
l
Sed
gew
ick
Pa
pa
dim
itri
ou
Mey
er
Ho
are
Dij
kst
ra
Cor
men
Too slow to be practical :/
2 / 8
Introductory example
Find Cormen when books are in alphabetical order
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Much faster if sorted! :)
3 / 8
Introductory example
Find Cormen when books are in alphabetical order
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Mey
er
Much faster if sorted! :)
3 / 8
Introductory example
Find Cormen when books are in alphabetical order
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Ga
mm
a
Much faster if sorted! :)
3 / 8
Introductory example
Find Cormen when books are in alphabetical order
Ab
elso
n
Ah
o
Bro
ok
s
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Cor
men
Much faster if sorted! :)
3 / 8
Introductory example
Find Cormen when books are in alphabetical order
Ab
elso
n
Ah
o
Bro
ok
s
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Cor
men
Much faster if sorted! :)
3 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, true
I Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set S
I Find a permutation P = [p1, p2, . . . , pN ], such that:I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= B
I For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤
I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Sorting – Formally
I Given a set S with total ordering : S× S→ false, trueI Reflexive: a aI Antisymmetric: (a b), (b a)⇒ (a = b)I Transitive: (a b), (b c)⇒ (a c)I Total: if not (a b) then (b a)
I Given a sequence of N items A1,A2, . . . ,AN , each from the set SI Find a permutation P = [p1, p2, . . . , pN ], such that:
I for all i ∈ [1;N − 1] it holds that Api Api+1
I In other words, construct a sequence B1,B2, . . .BN , such that:I Every Bi has exactly one corresponding Aj , and vice versaI Bi Bi+1
I Note:I Strict ordering: A ≺ B := A B and A 6= BI For numbers (S = Z,R, . . .), is often ≤I We will denote as ≤, and ≺ as <, in the rest of the week materials
4 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering1. Does this book exist?
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering1. Does this book exist? Gamma
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering1. Does this book exist? Gamma → YES
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
8G
am
ma
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering1. Does this book exist? Kant
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering1. Does this book exist? Kant → NO
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
11
Ho
are
12
Ker
nig
ha
n
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering2. How many books of Patashnik is there?
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering2. How many books of Patashnik is there? Two
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
18
Pa
tash
nik
19
Pa
tash
nik
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering3. How many books have a name smaller than Ritchie?
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
20
Rit
chie
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast query answering3. How many books have a name smaller than Ritchie? 19 (the index minus 1)
1A
bel
son
2A
ho
3B
roo
ks
4C
orm
en
5D
ijk
stra
6E
ckel
7F
oris
ek
8G
am
ma
9G
od
el
10
Gra
ha
m
11
Ho
are
12
Ker
nig
ha
n
13
Kn
uth
14
La
mp
ort
15
McC
on
nel
l
16
Mey
er
17
Nor
vig
18
Pa
tash
nik
19
Pa
tash
nik
21
Ru
ssel
22
Set
hi
23
Sed
gew
ick
24
Sk
ien
a
25
Sp
ols
ky
26
Str
ou
stru
p
27
Su
ssm
an
28
Ta
nen
ba
um
29
Tu
rin
g
30
Wir
th
20
Rit
chie
5 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processing
Given segment endpoints where paint was applied. Find which length was painted
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied.
Find which length was painted
1 4
2 5
8 9
7 11
10 12
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinate
I Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Prepare to fast data processingGiven segment endpoints where paint was applied. Find which length was painted
1 4
2 5
8 9
7 11
10 12
Solution:
I Sort segments by the left coordinateI Traverse segments in the sorted order
I Track the endpoint of the current segment clusterI Finish the cluster when the next segment is beyond this point
6 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solution
7 1 4 6 8 2 9 3 1 4 3 5 9 8 2
3 1 2 8 6 5 4 7 4 9 4 5 1 3 8
7 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solutionExample:
I Given two sequences A = [A1, . . .AN ] and B = [B1, . . .BN ]
I Find permutations P and Q such that∑N
i=1 APi· BQi
is maximum possible
7 1 4 6 8 2 9 3 1 4 3 5 9 8 2
3 1 2 8 6 5 4 7 4 9 4 5 1 3 8
7 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solutionExample:
I Given two sequences A = [A1, . . .AN ] and B = [B1, . . .BN ]
I Find permutations P and Q such that∑N
i=1 APi· BQi
is maximum possible
Solution:
I Sort A in non-decreasing order
I Sort B in non-decreasing order
I These facts will be proven in a this week’s video
7 1 4 6 8 2 9 3 1 4 3 5 9 8 2
3 1 2 8 6 5 4 7 4 9 4 5 1 3 8
7 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solutionExample:
I Given two sequences A = [A1, . . .AN ] and B = [B1, . . .BN ]
I Find permutations P and Q such that∑N
i=1 APi· BQi
is maximum possible
Solution:
I Sort A in non-decreasing order
I Sort B in non-decreasing order
I These facts will be proven in a this week’s video
1 1 2 2 3 3 4 4 5 6 7 8 8 9 9
3 1 2 8 6 5 4 7 4 9 4 5 1 3 8
7 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solutionExample:
I Given two sequences A = [A1, . . .AN ] and B = [B1, . . .BN ]
I Find permutations P and Q such that∑N
i=1 APi· BQi
is maximum possible
Solution:
I Sort A in non-decreasing order
I Sort B in non-decreasing order
I These facts will be proven in a this week’s video
1 1 2 2 3 3 4 4 5 6 7 8 8 9 9
1 1 2 3 3 4 4 4 5 5 6 7 8 8 9
7 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solutionExample:
I Given two sequences A = [A1, . . .AN ] and B = [B1, . . .BN ]
I Find permutations P and Q such that∑N
i=1 APi· BQi
is minimum possible
Solution:
I Sort A in non-decreasing order
I Sort B in non-increasing order
I These facts will be proven in a this week’s video
1 1 2 2 3 3 4 4 5 6 7 8 8 9 9
9 8 8 7 6 5 5 4 4 4 3 3 2 1 1
7 / 8
Why sorting?
Why do we need sorting? – Construct an optimal solutionExample:
I Given two sequences A = [A1, . . .AN ] and B = [B1, . . .BN ]
I Find permutations P and Q such that∑N
i=1 APi· BQi
is minimum possible
Solution:
I Sort A in non-decreasing order
I Sort B in non-increasing order
I These facts will be proven in a this week’s video
1 1 2 2 3 3 4 4 5 6 7 8 8 9 9
9 8 8 7 6 5 5 4 4 4 3 3 2 1 1
7 / 8
What will we learn this week for sorting?
I Insertion sort: a simple sorting algorithm
I If a sorted sequence yields an optimal result, how to prove it?
I Quick sort: a very fast algorithm
I Merge sort: can never be too slow
I Stable and unstable sorting algorithms
I How to compare various objects
I An Ω(n log n) bound on the complexity of comparison-based algorithms
I Bucket sort and radix sort: Linear non-comparison sorting algorithms
I Sorting algorithms in standard libraries
8 / 8