Best case
Ω
(
n
)
\Omega(n)
Ω(n)
The best case happens when the array is already sorted. Then for each element in the array, it enters the loop and exits at once.
worst case
O
(
n
2
)
O(n^2)
O(n2)
(
n
−
1
)
+
(
n
−
2
)
+
.
.
.
+
1
=
(
1
/
2
)
n
2
+
(
1
/
2
)
n
=
O
(
n
2
)
(n-1)+(n-2)+...+1 = (1/2)n^2+(1/2)n = O(n^2)
(n−1)+(n−2)+...+1=(1/2)n2+(1/2)n=O(n2)
The worst case happens when the array is reverse ordered. For every cycle, only one element is sorted into the right place.
Average time complexity
Discuss the case of n elements. In the first loop, we compare (n-1) times and then we separate the array into 2 partitions.
For the sake of convenience, we represent the time complexity of sorting an n-element array by T(n).
Because the elements in the array is in random order, the possibility of each partition result is equal as
1
n
\frac{1}{n}
n1,because there are total n partition results.
Thus, we have the recurrent formula:
T
(
n
)
=
(
n
−
1
)
+
∑
k
=
1
n
−
1
(
T
(
k
)
+
T
(
n
−
k
)
)
n
T(n) = (n-1) + \frac{\sum_{k=1}^{n-1}(T(k) + T(n-k))}{n}
T(n)=(n−1)+n∑k=1n−1(T(k)+T(n−k))
Write down some cases of
∑
k
=
1
n
−
1
(
T
(
k
)
+
T
(
n
−
k
)
)
\sum_{k=1}^{n-1}(T(k) + T(n-k))
∑k=1n−1(T(k)+T(n−k)) and we find that the formula can be transformed to
T
(
n
)
=
(
n
−
1
)
+
2
∑
k
=
1
n
−
1
T
(
k
)
n
,
n
≥
1
T(n) = (n-1) + 2\frac{\sum_{k=1}^{n-1}T(k)}{n} ,n\ge1
T(n)=(n−1)+2n∑k=1n−1T(k),n≥1
with the perturbation method(下标扰动法)/ (列出几项寻找规律也可以)
Rewrite the formula and set n to n+1:
T
(
n
+
1
)
=
(
n
)
+
2
∑
k
=
1
n
T
(
k
)
n
+
1
,
n
≥
1
T(n+1) = (n) + 2\frac{\sum_{k=1}^{n}T(k)}{n+1} ,n\ge1
T(n+1)=(n)+2n+1∑k=1nT(k),n≥1
Do subtraction
(
3
)
×
(
n
+
1
)
−
(
2
)
×
n
(3)\times (n+1) - (2)\times n
(3)×(n+1)−(2)×n:
(
n
+
1
)
T
(
n
+
1
)
−
n
T
(
n
)
=
2
n
+
2
T
(
n
)
,
n
≥
1
(n+1)T(n+1) - nT(n) = 2n + 2T(n) , n\ge 1
(n+1)T(n+1)−nT(n)=2n+2T(n),n≥1
(
n
+
1
)
T
(
n
+
1
)
−
(
n
+
2
)
T
(
n
)
=
2
n
,
n
≥
1
(n+1)T(n+1) - (n+2)T(n) = 2n , n\ge 1
(n+1)T(n+1)−(n+2)T(n)=2n,n≥1
Set n to n-1 and rewrite the formula,
(
n
)
T
(
n
)
−
(
n
+
1
)
T
(
n
−
1
)
=
2
(
n
−
1
)
,
n
≥
2
(n)T(n) - (n+1)T(n-1) = 2(n-1) , n\ge 2
(n)T(n)−(n+1)T(n−1)=2(n−1),n≥2
Use the summation factor method to get the summation factor
s
n
s_n
sn:
for formula
a
n
T
n
=
b
n
T
n
−
1
+
c
n
a_nT_n = b_nT_{n-1} + c_n
anTn=bnTn−1+cn
According to the definition of the summation factor, summation factor
s
n
s_n
sn satisfies:
s
n
−
1
a
n
−
1
=
s
n
b
n
s_{n-1}a_{n-1} = s_nb_n
sn−1an−1=snbn
Here
a
n
=
n
,
b
n
=
n
+
1
,
c
n
=
2
(
n
−
1
)
a_n = n ,b_n = n+1, c_n = 2(n-1)
an=n,bn=n+1,cn=2(n−1):
Thus,
s
n
=
a
1
a
2
.
.
.
a
n
b
2
b
3
.
.
.
b
n
+
1
=
2
(
n
)
(
n
+
1
)
s_n = \frac{a_1a_2...a_n}{b_2b_3...b_{n+1}} = \frac{2}{(n)(n+1)}
sn=b2b3...bn+1a1a2...an=(n)(n+1)2
Let
S
n
=
a
n
T
n
s
n
=
2
n
(
n
)
(
n
+
1
)
T
(
n
)
S_n = a_nT_ns_n = \frac{2n}{(n)(n+1)}T(n)
Sn=anTnsn=(n)(n+1)2nT(n),we have
S
n
=
S
n
−
1
+
4
(
n
−
1
)
(
n
)
(
n
+
1
)
,
n
≥
2
S_n = S_{n-1} + \frac{4(n-1)}{(n)(n+1)} ,n\ge2
Sn=Sn−1+(n)(n+1)4(n−1),n≥2
S
1
=
(
1
/
3
)
T
(
1
)
=
0
S_1 = (1/3)T(1) = 0
S1=(1/3)T(1)=0
Thus,
S
n
=
∑
k
=
2
n
4
k
k
(
k
+
1
)
=
4
∑
k
=
2
n
1
k
+
1
S_{n} = \sum_{k=2}^n \frac{4k}{k(k+1)} = 4\sum_{k=2}^n \frac{1}{k+1}
Sn=k=2∑nk(k+1)4k=4k=2∑nk+11
Because
H
n
=
1
+
1
/
2
+
1
/
3
+
.
.
.
+
1
/
n
H_n = 1 + 1/2 + 1/3 + ... +1/n
Hn=1+1/2+1/3+...+1/n,
S
n
=
4
(
H
n
+
1
n
+
1
−
1
)
S_{n} = 4(H_n+\frac{1}{n+1}-1)
Sn=4(Hn+n+11−1)
Now we finally switch back to T(n),
T
(
n
)
=
S
n
s
n
a
n
T(n) = \frac{S_n}{s_na_n}
T(n)=snanSn
=
S
n
(
n
+
1
)
2
n
=\frac{Sn(n+1)}{2n}
=2nSn(n+1)
=
2
(
n
+
1
)
(
H
n
+
1
n
+
1
−
1
)
=2(n+1)(H_n+\frac{1}{n+1}-1)
=2(n+1)(Hn+n+11−1)
=
2
(
n
+
1
)
H
n
−
2
n
=2(n+1)H_n - 2n
=2(n+1)Hn−2n
Because
H
n
=
1
+
1
/
2
+
1
/
3
+
.
.
.
+
1
/
n
≈
l
n
n
H_n = 1 + 1/2 + 1/3 + ... +1/n \approx lnn
Hn=1+1/2+1/3+...+1/n≈lnn,
≈
2
(
n
+
1
)
l
n
n
−
2
n
\approx 2(n+1)lnn - 2n
≈2(n+1)lnn−2n
=
O
(
n
l
n
n
)
=O(nlnn)
=O(nlnn)
快速排序时间复杂度分析-最好、最坏、平均
最新推荐文章于 2024-06-18 20:29:24 发布