算法三-----QUICKSORT

代码:
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元素的分界线,j 指向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 A[j]<p: [ 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[i1] A [ l + 1 ] , . . . A [ i − 1 ] are all less than the pivot.
3.2. A[i],...,A[j1] 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(σ)=n1i=1nj=i+1Xij(σ) ∀ σ , C ( σ ) = ∑ i = 1 n − 1 ∑ j = i + 1 n X i j ( σ )
By linearity of expectations:
E[C]=n1i=1nj=i+1E[Xij] E [ C ] = ∑ i = 1 n − 1 ∑ j = i + 1 n E [ X i j ]
And: E[Xij]=0P[Xij=0]+1P[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]=n1i=1nj=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]=2ji+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]=n1i=1nj=i+12ji+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 iSp(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 ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值