Question 2:
The worst-case running time for quicksort is given by the recurrence T
(
n
)
=
T
(
n
−
1
)
+
Θ
(
n
)
.
Use
the substitution method to prove that this recurrence has the solution T
(
n
)
=
Θ
(
n
2
)
, as claimed at
the beginning of section 7.2.
Solution:
The worst-case running time for quicksort is given by the recurrence T
(
n
)
=
T
(
n
−
1
)
+
Θ
(
n
)
is
T
(
n
)
=
Θ
(
n
2
)
.
To Prove it using the substitution method, we must prove that T
(
n
)
≤c .n
2
.
Given:
T
(
n
)
=
T
(
n
−
1
)
+
Θ
(
n
)
.
let n=1,
T
(
1
)
=
Θ
(
1
)
. Assume T
(
1
)
as a constant.
As, T
(
n
)
≤c .n
2
Assume n=k, T
(
n
)
≤c .k
2
where c>0, for all k>n.
To prove T
(
n
)
≤c .n
2
for the given recurrence relation,
T
(
n
)
=
T
(
n
−
1
)
+
Θ
(
n
)
---------
equation 1
and T
(
n
)
≤c .k
2
------------------ -
equation 2
let k=n-1 and compare equation 1 and 2 The attained inequality is T
(
n
)
≤c
(
n
−
1
)
2
+
Θ
(
n
)
solving the inequality, T
(
n
)
≤c
(
n
2
−
2
n
+
1
)+
Θ
(
n
)
T
(
n
)
≤c n
2
−
2
nc
+
c
+
Θ
(
n
)
, c can be absorbed into Θ
(
n
)
as it's a constant.
Hence, we get
T
(
n
)
≤c n
2
+
Θ
(
n
)
, holds true for n≥
1
Conclusion,
By the following steps, we can conclude that the recurrence relation T
(
n
)
=
T
(
n
−
1
)
+
Θ
(
n
)
.
, has a
solution of T
(
n
)
=
Θ
(
n
2
)
under certain conditions. This implies that the worst-case running time
for the quicksort is T
(
n
)
=
Θ
(
n
2
)
.