find ith smallest element in an array
来源:互联网 发布:分类信息系统源码 编辑:程序博客网 时间:2024/05/17 23:52
Order Statistics
given n elements in array
find
Naive algorithm:
1. sort A
2. return A[k-1]
Here find median of an array.
Randomized divide and conquer
Intuition for analysis
(Assume elements are distinct)
Lucky case: 1/10 9/10
We’re doing the worst-case analysis within the lucky cases.
Unlucky case: 0 :n-1
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
-
Claim:
Proof
Substitution Method
Assume true for
E[T(n)]
Rand-Select has
Worst-case linear-time order statistics
[Blum,Floyd,Pratt,Rivest and Tarjan]
- guarantee good pivot recursively
Select(i,n):
- Divide the n elements
[n/5] groups of 5 elements.
Find the median of each group.Θ(n) - Recursively select the median x of the [n/5] group medians. T(n/5)
- Partition with x as pivot.Let k = rank(x)
if i = k then return x
if i< k then recursively selectith smallest element in the left .
if i< k then recursively selectith 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
T(k)
So
T(n)
if 1/20cn
So the running time is
Code for IthElement in github:
code in github
- find ith smallest element in an array
- [leetcode]Kth Smallest Element in an Array
- PIQ03: Find Majority Element in an Array
- quick select to find the kth smallest element in array
- Find the kth largest element in an unsorted array
- [LeetCode] 4 Find the kth smallest element in two sorted array
- Find the maxest element of an array
- Find duplicates in an array
- 编程之 最小的K个数 216 Kth Largest Element in an Array 378. Kth Smallest Element in a Sorted Matrix
- 代码面试题:Find two smallest number of an array
- Find the smallest positive number missing from an unsorted array
- Finding Second Minimum element in an Array
- Kth Largest Element in an Array
- Leetcode Kth Largest Element in an Array
- Kth Largest Element in an Array
- Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- 重写的easyui-validatebox 验证规则
- ECLIPSE(JAVA, SCALA)中TAB WIDTH的设置
- Matlab PCA+SVM人脸识别(二)——GUI界面设计
- 查看目录占用空间及有多少文件
- /lib /usr/lib /usr/local/lib区别
- find ith smallest element in an array
- 贪心算法的基本思想
- 大小端模式详解
- <c:forEach >中如何显示序号
- java 静态代码块 构造代码块 构造方法的执行顺序
- Windows MySQL5.7安装和配置
- python获取图片
- 结构-行为-样式-Bootstrap笔记
- 测试一个字符串中各字符出现的次数