0% found this document useful (0 votes)
6 views12 pages

WK 11

Uploaded by

efe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

WK 11

Uploaded by

efe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Problem 1

A 5,8,2,0,2了 786 2,65

into 2 halves
Merge Sati Divides
inputang

call itself for the 2 halves then the


merge

two sorted halves

Time i Th 2 尫 0 n

solve it either recurrence tree or maser


using

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

In total running time

Divide cost in arms


step in
Rear cost in arms of T snag
step

Conquer
step cost in terms of n

Also set a base case


up

Therefore

Divide and
Rear
fml
t

TY
conquer

Base Case OLD


gag fml
Y In
Th a 结⽵ 们

Did Ol n time need to


i
aping copying
Recursion 2 parts each of size I 2
TH
Conquer i hate 2
arrays copy over at
least one

ofthe tno soc.nl

Base case when ml it


just return

但 1 0⼼
121
form
So Ty
Out fml
Sohe recurrence by unrolling
I Analyze first few levels
2 Identify the pattern for ageneric level

3 Sum our all levels


up

Similar to Merge sat lecture Ps


Th a 24 Cx It 4 xc ⼼ 0
Ghg
rita 㲭 ibihgu i.gg

㸠⼼ A in objects

whether there is an element appears more than E times

time
Olngn
a Pigeonhole principle
if niamsarepu.int
m containers with mm then at least one

container must contain more than liam

if x
appears more than
是 times inthe original

away
then i must be a majority element in g
g l of the 2 halves

x is majority at least I 1 entries

qnaltox
xxxnn
_ oxxxxxe_rrr
in
n

10 00 1
8 holes atleast 4 111 5 entries mustbe x

so lath fisthalf回 second


half must contain
f 1 3 entries of x

this will make x a element within that


majority half
b San the array how
counting
mayans equal
to x count is more than 是
If declare x to

be a
majority
element On

a Divide break Aint 2 has

Recursion recurse on each


half
there is element will be returned
Conquer test
if an

method 的 andcombineanswer
using

from left and right haha


otherwise no element
majority

kune on lefttight hehe of the input unit


each has a
length 1 In this case th
part
element is just itself the recursionstopshere
majority

When current slice is longer than I must combine

the answer
for left and right des If g
on th majority
element then the
agree majority

element for the overall slice is the same


If
they disagree only one of them is correct So

We need to count the of both


occurrence
Gt
and right majority elements to claim whichis

globally correct

In 2ㄒ艺 0
0 n
agn
Problem
4 Count inversions
using merge
sort

A Get number of inversions in the merge


step
i used
used
for lefthay j frighthag
than 咧
Left authese arebigger Right
i_erryifaibID
at.is 咧
if left ⻔ Right D

then there are hid y inversions

Because are sorted


left lright.subangs
so

the remaining elements in left away


iii a T.it 2 i.ci mid will be
greater than


by

it is the same as merge

sot hn

5 i
sort A
ang
distinct non negative integers

that isn't sorted


there is an
integer Aūy
A 8 i 5.7 run 2

f

2

AE AI2 lJ z lAI3ts3
AIt0
然 AE 3
Modified Binary Search i consider the

middle element domine which contains


half
a missing element and recurse on th he

only on a half
Base case
def function
A
how
⾏以
get 幽
if bihghi
reunion
if A Img mid

Then there can't be elements in


any missing
the mid
ft
elements

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

⼆ 0 Clog Binay search

You might also like