find ith smallest element in an array

来源:互联网 发布:分类信息系统源码 编辑:程序博客网 时间:2024/05/17 23:52

Order Statistics

given n elements in array
find kthsmallest element(element of rank k)

Naive algorithm:
1. sort A
2. return A[k-1]
T(n)=Θ(nlog2n)

Here find median of an array.

Randomized divide and conquer

这里写图片描述
这里写图片描述

Intuition for analysis

(Assume elements are distinct)
Lucky case: 1/10 9/10
T(n)T(9/10)+Θ(n)
We’re doing the worst-case analysis within the lucky cases.
T(n)=Θ(n)

Unlucky case: 0 :n-1
T(n)=Θ(n)+T(n1)=Θ(n2)

Analysis of expected time
- Let T(n) be the random variable for running time of rand-select on input of size n.Assume random numbers are independent.
- Define indicator random variable Xk k from 0 to n-1
- Xk=

1,0,if partition generates an k to n-k-1 splitotherwise

T(n)=

T(max(0:n1))+Θ(n),T(max(1:n2))+Θ(n),...T(max(n1:0))+Θ(n)if 0:n-1 splitif 1:n-2 splitif n-1:0 split

T(n)=n10Xk(T(max(k,nk1))+Θ(n))
E[T(n)]=E[n10Xk(T(max(k,nk1))+Θ(n))]=n10E[Xk(T(max(k,nk1))+Θ(n))]=n10E[Xk]E[T(max(k,nk1))+Θ(n))]=n101/nE[T(max(k,nk1))+Θ(n))]=n101/nE[T(max(k,nk1))]+n101/nΘ(n)2/nn1[n/2]E[T(k)]+Θ(n)

Claim:E[T(n)]cn for constant c>0

Proof

Substitution Method
Assume true for <n
E[T(n)]2/nn1[n/2]E[T(k)]+Θ(n)2/nn1[n/2]ck+Θ(n)2c/n3/8n2+Θ(n)=cn(1/4cnΘ(n))cn if 1/4cnΘ(n)

Rand-Select has Θ(n) expect running time.
Θ(n2)worst running time.

Worst-case linear-time order statistics

[Blum,Floyd,Pratt,Rivest and Tarjan]
- guarantee good pivot recursively
Select(i,n):

  1. Divide the n elements
    [n/5] groups of 5 elements.
    Find the median of each group. Θ(n)
    这里写图片描述
  2. Recursively select the median x of the [n/5] group medians. T(n/5)
  3. Partition with x as pivot.Let k = rank(x)
  4. if i = k then return x
    if i<k then recursively select ith smallest element in the left .
    if i <k then recursively select ith smallest element in the right. T(3n/4)

    这里写图片描述
    这里写图片描述
    So running time in the step 4 is T(3n/4)
    T(n)T(3n/4)+T(n/5)+Θ(n)
    Claim: T(n)cn

Proof:substitution

Assume true for <n
T(k)ck
So
T(n)3cn/4+cn/5+Θ(n)19/20cn+Θ(n)cn(1/20cnΘ(n))cn
if 1/20cn>Θ(n)
So the running time is Θ(n)

Code for IthElement in github:
code in github

0 0
原创粉丝点击