CSE 150 Fall 2009: Homework #4
Chao Xu
Problem 1
•
T
(
n
) =
T
(
n/
2
) + 1 = Θ(log
n
)
•
T
(
n
) = 2
T
(
n/
2
) + 1 = Θ(
n
)
•
T
(
n
) = 4
T
(
n/
2
) + 1 = Θ(
n
2
)
•
T
(
n
) =
T
(
n
−
1) + 1 = Θ(
n
) [This is
T
(
n
) =
n
written recursively]
•
T
(
n
) = 2
T
(
n
−
1) + 1 = Θ(2
n
) [2
n
≤
T
(
n
)
≤
3
n
]
•
T
(
n
) =
T
(
n/
2
) +
n
= Θ(
n
)
•
T
(
n
) = 4
T
(
n/
2
) +
n
= Θ(
n
2
)
Problem 2
The following will use
A
[
i,j
] for
A
[
i,...,j
] and if
i > j
, it return nil.
procedure
f
(
A
[0
,n
−
1][0
,m
−
1]
,x
)
if
A
=
nil
return
false
i
←
n
2
j
←
m
2
k
←
A
[
i
][
j
]
if
m
= 1
∧
n
= 1
return
k
=
x
if
x < k
return
f
(
A
[0
,i
][0
,j
]
,x
)
∨
f
(
A
[0
,i
][
j
+ 1
,m
−
1]
,x
)
∨
f
(
A
[
i
+ 1
,n
−
1][0
,j
]
,x
)
if
x > k
return
f
(
A
[
i
+ 1
,n
−
1][
j
+ 1
,m
−
1]
,x
)
∨
f
(
A
[0
,i
][
j
+ 1
,m
−
1]
,x
)
∨
f
(
A
[
i
+ 1
,n
−
1][0
,j
]
,x
)
return
true
T
(
mn
) = 3
T
(
mn
4) + 1
T
(
mn
) = Θ(
mn
log
4
3
)
<
Θ(
mn
)
Note: There exist a
Θ(
m
+
n
)
algorithm, but I fail to think of it when I hand in the hw.
Problem 3
procedure
insert(
L,x
)
if
x
≤
L.data
∨
L
=
nilN.data
←
xN.next
←
L
return
N L.next
= insert(
L.next,x
)
return
L
1
Add a Comment