WK 11
WK 11
into 2 halves
Merge Sati Divides
inputang
Time i Th 2 尫 0 n
method 0
Gbgn
5 8 2 0
123 786 2 65
5 8 1 20 23 786 l 2 65
1
5 8 210 2317 2
l65
fi i Sh It
shes.ie
Then merge
䥚 品 后
0258 2365786
t.zponwn.ge
2,0,2,5,8,23.65,786
Problem 2
Solve a recurrence
Conquer
step cost in terms of n
Therefore
Divide and
Rear
fml
t
TY
conquer
但 1 0⼼
121
form
So Ty
Out fml
Sohe recurrence by unrolling
I Analyze first few levels
2 Identify the pattern for ageneric level
㸠⼼ A in objects
time
Olngn
a Pigeonhole principle
if niamsarepu.int
m containers with mm then at least one
if x
appears more than
是 times inthe original
away
then i must be a majority element in g
g l of the 2 halves
qnaltox
xxxnn
_ oxxxxxe_rrr
in
n
10 00 1
8 holes atleast 4 111 5 entries mustbe x
be a
majority
element On
method 的 andcombineanswer
using
the answer
for left and right des If g
on th majority
element then the
agree majority
globally correct
In 2ㄒ艺 0
0 n
agn
Problem
4 Count inversions
using merge
sort
们
by
sot hn
5 i
sort A
ang
distinct non negative integers
f
个
2
AE AI2 lJ z lAI3ts3
AIt0
然 AE 3
Modified Binary Search i consider the
only on a half
Base case
def function
A
how
⾏以
get 幽
if bihghi
reunion
if A Img mid
so recurse on
Right half
A mid 1 high
if Ahidjo i
missing values
There are some
Deane on
Gt half
A low mid
Time
矵 is4 Yyz_Divide
0 y
tongue
O D