N2, 2N, 25, Nlg(lgN), Nlg(N2),
N2lgN, N3, NlgN,
and N!
.
T(N)
given that T(1) = θ(1)
:
T(N) = 2N - 1 + T(N-1)
T(N) = N + T(N-3)
T(N) = N2 + T(N-1)
a. sum = 0; for (int i = 0; i < N; i++) for (int j = 0; j < i * i; j++) for (int k = 0; k < j; k++) sum++; b. sum = 0; for (int i = 0; i < N; i++) for (int j = 0; j < i * i; j++) if (j % i == 0) { for (int k = 0; k < j; k++) sum++; }
θ(NlgN)
but its
overhead (hidden in the constant factors) is high and this is manifested near
the bottom of the recursion tree where many merges are made. Someone proposed
that we stop the recursion once the size reaches K
and switch to
insertion sort at that point. Analyze this proposal (by modifying the
recurrence analysis of standard mergesort) and prove that its running time
is θ(NK + Nlg(N/K))
. How would you choose K
?
A
of N
distinct integers,
we seek an algorithm to determine if there exists an index i
such
that A[i] = i
.
θ
running time in
the worst case.
θ
running time in
the worst case.
N
nodes and height H
:
null
links in the tree is N+1
.
N ≤ 2H+1 - 1
.
ambmcpdse
and smmabpcde
respectively, draw
the tree and list its nodes in postorder.
1, 2, ... N
, i.e. the integers
1
to N
sorted,
describe a linear algorithm to store
them in an N
-node BST so that the BST will be perfectly balanced
with height H
. Assume N = 2H+1 - 1
.