How to Win Coding Competitions: Secrets of Champions
Week 3: Sorting and Search AlgorithmsLecture 10: Introduction to binary search
Maxim BuzdalovSaint Petersburg 2016
Binary search
Recall the example from Lecture 1: how to search in sorted data
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
This is an example of binary search. We will have more in this video
2 / 6
Binary search
Recall the example from Lecture 1: how to search in sorted data
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
This is an example of binary search. We will have more in this video
2 / 6
Binary search
Recall the example from Lecture 1: how to search in sorted data
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
This is an example of binary search. We will have more in this video
2 / 6
Binary search
Recall the example from Lecture 1: how to search in sorted data
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
This is an example of binary search. We will have more in this video
2 / 6
Binary search
Recall the example from Lecture 1: how to search in sorted data
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
This is an example of binary search. We will have more in this video
2 / 6
Binary search – Problem to solve
A very general form of binary search
I Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operation
I We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and b
I D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operation
I We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and b
I D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operation
I We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and b
I D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find where q is in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” if q = x , “−1” if x < q, “+1” if x > q
I If no such element, “0” is infeasible: then you will find where to insert q
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find where q is in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” if q = x , “−1” if x < q, “+1” if x > q
I If no such element, “0” is infeasible: then you will find where to insert q
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find where q is in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” if q = x , “−1” if x < q, “+1” if x > q
I If no such element, “0” is infeasible: then you will find where to insert q
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find first occurence of q in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” never, “−1” if x < q, “+1” if x ≥ q
I But first check if F (y) = q. . .
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find first occurence of q in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” never, “−1” if x < q, “+1” if x ≥ q
I But first check if F (y) = q. . .
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find first occurence of q in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” never, “−1” if x < q, “+1” if x ≥ q
I But first check if F (y) = q. . .
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find a root of a monotonically growing function f
I D = [min; max]: the segment of R which we are interested in
I Avg(a, b) = (a + b)/2
I F (x): “0” if f (x) = 0, “−1” if f (x) < 0, “+1” if f (x) > 0
I Warning: you are unlikely to find the root exactly. . .
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find a root of a monotonically growing function f
I D = [min; max]: the segment of R which we are interested in
I Avg(a, b) = (a + b)/2
I F (x): “0” if f (x) = 0, “−1” if f (x) < 0, “+1” if f (x) > 0
I Warning: you are unlikely to find the root exactly. . .
4 / 6
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find a root of a monotonically growing function f
I D = [min; max]: the segment of R which we are interested in
I Avg(a, b) = (a + b)/2
I F (x): “0” if f (x) = 0, “−1” if f (x) < 0, “+1” if f (x) > 0
I Warning: you are unlikely to find the root exactly. . .
4 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R)if Vmin = 1 then return 〈Null, Dmin〉 end ifif Vmax = −1 then return 〈Dmax,Null〉 end ifif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end ifif Vmax = −1 then return 〈Dmax,Null〉 end ifif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end ifif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end if . Direct hit!if v = −1 then L← M else R ← M end if
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end if . Direct hit!if v = −1 then L← M else R ← M end if . “Too early”: use right part
end forend function
5 / 6
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end if . Direct hit!if v = −1 then L← M else R ← M end if . “Too early”: use right part
end for . “Too late”: use left partend function
5 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of Avg
I If Avg is a “real” average, the running time is O(logD)I the size of [L;R) range is divided by two on every iteration
6 / 6
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6