代码:
https://github.com/Shinered/quicksort
1.Partitioning Around a Pivot
Key idea: partition array around a pivot element
(1) pick element of array
(2) Rearrange array so that:
a. left of pivot —>less than pivot
b.right of pivot——->greater than pivot
(3) QuickSort(array A, length n)
– if n = 1, return
– p = ChoosePivot(A,n)
– Partition A around p
– recursively sort 1st part.
2.In-PLace Implementation
Assume: pivot=1st element of array
[if not, swap pivot <—-> 1st element as preprocessing step]
Pseudocode for Partition.. Pivot: 主轴
Partition(A, l, r):
[input=A[l,...r]]
[
i
n
p
u
t
=
A
[
l
,
.
.
.
r
]
]
i
i
指向小于pivot和大于pivot元素的分界线, 指向partitioned和unpartitioned元素的分界线。
p=A[l]
p
=
A
[
l
]
i=l+1
i
=
l
+
1
for
j=l+1
j
=
l
+
1
to
r
r
–if [
ifA[j]>p,
i
f
A
[
j
]
>
p
,
do nothing.]
–Swap A[j] and A[i]
–
i=i+1
i
=
i
+
1
3.Correctness:
3.1.
A[l+1],...A[i−1]
A
[
l
+
1
]
,
.
.
.
A
[
i
−
1
]
are all less than the pivot.
3.2.
A[i],...,A[j−1]
A
[
i
]
,
.
.
.
,
A
[
j
−
1
]
are all greater than pivot.
conquence: at end of for loop, have after final swap, array partitioned around pivot.
1. Choose pivots
Quick sort Theorem: for every input array of lenght n,the average running time of Quick sort (with random pivots) is O(n logn).
4.1. Analysis about random pivots
Fix input array A of length n.
Samle space
Ω=
Ω
=
all possible outcomes of random choices in QuickSort(pivot sequences).
Key random varible: for
σ∈Ω,
σ
∈
Ω
,
C(σ)=
C
(
σ
)
=
#of comparisons between two input elements made by QuickSort (given random choices
σ
σ
)
lemma: running time of QuickSort dominated by comparisons.
4.2 Building blocks:
Note: cant’t apply master method.
[A = fixed input array]
Notation:
zi=ith
z
i
=
i
t
h
smallest element of A.
For
σ∈Ω
σ
∈
Ω
,indices
i<j
i
<
j
,let:
Xij(σ)=
X
i
j
(
σ
)
=
#of times
zi,zj
z
i
,
z
j
get compared in QuickSort with pivot sequence
A Decomposition Approach.
So:
C(σ)=
C
(
σ
)
=
# comparisons between input elements.
Xij=
X
i
j
=
# comparisons between
zi
z
i
and
zj
z
j
.
Thus:
∀σ,C(σ)=∑n−1i=1∑nj=i+1Xij(σ)
∀
σ
,
C
(
σ
)
=
∑
i
=
1
n
−
1
∑
j
=
i
+
1
n
X
i
j
(
σ
)
By linearity of expectations:
E[C]=∑n−1i=1∑nj=i+1E[Xij]
E
[
C
]
=
∑
i
=
1
n
−
1
∑
j
=
i
+
1
n
E
[
X
i
j
]
And:
E[Xij]=0⋅P[Xij=0]+1⋅P[Xij=1]=P[Xij=1]
E
[
X
i
j
]
=
0
⋅
P
[
X
i
j
=
0
]
+
1
⋅
P
[
X
i
j
=
1
]
=
P
[
X
i
j
=
1
]
Thus:
E[C]=∑n−1i=1∑nj=i+1P[zi,zjgetcomplicated]
E
[
C
]
=
∑
i
=
1
n
−
1
∑
j
=
i
+
1
n
P
[
z
i
,
z
j
g
e
t
c
o
m
p
l
i
c
a
t
e
d
]
Key claim:
∀i<j,P[zi,zjgetcompared]=2j−i+1
∀
i
<
j
,
P
[
z
i
,
z
j
g
e
t
c
o
m
p
a
r
e
d
]
=
2
j
−
i
+
1
Proof of Key Claim
Fixed
zi,zj
z
i
,
z
j
with
i<j
i
<
j
.
So:
E[C]=∑n−1i=1∑nj=i+12j−i+1
E
[
C
]
=
∑
i
=
1
n
−
1
∑
j
=
i
+
1
n
2
j
−
i
+
1
5.Concept#1-Sample Spaces
5.1 Sample space
Ω=
Ω
=
“all possible outcomes”
[in algorithms,
Ω
Ω
is usually finite]
Also: each outcome
i∈Ω
i
∈
Ω
has a probablity
p(i)>=0
p
(
i
)
>=
0
Contraint:
∑i∈Ω=1
∑
i
∈
Ω
=
1
Example #1: Rolling 2 dice.
36 ordered pairs:
Ω={(1,1),(2,1),(3,1),...,(5,6),(6,6)}
Ω
=
{
(
1
,
1
)
,
(
2
,
1
)
,
(
3
,
1
)
,
.
.
.
,
(
5
,
6
)
,
(
6
,
6
)
}
and
p(i)=136
p
(
i
)
=
1
36
for all
i∈Ω
i
∈
Ω
Example #2: Choosing a random pivot in outer QuickSort call.
Ω={1,2,...n}
Ω
=
{
1
,
2
,
.
.
.
n
}
and
p(i)=1n
p
(
i
)
=
1
n
for all
i∈Ω
i
∈
Ω
5.2 Concept #2-Events
An event is a subset
S⊆Ω
S
⊆
Ω
The probablity of an event S is
∑i∈Sp(i)
∑
i
∈
S
p
(
i
)
5.3 Concept #5-Linearity of Expectation
Claim[LIN EXP]: let
x1,....,xn
x
1
,
.
.
.
.
,
x
n
be random varibles.
defined on
Ω
Ω
.Then:
E[∑ni=1xi]=∑ni=1E[xi]
E
[
∑
i
=
1
n
x
i
]
=
∑
i
=
1
n
E
[
x
i
]
算法三-----QUICKSORT
最新推荐文章于 2020-01-23 21:28:55 发布