Selection Problem (选择问题)
在一个由n个元素组成的集合中,第i个“顺序统计量(order statistic)”是该集合中第i小的元素。例如,在一个由n个元素组成的集合中,最小值是第1个顺序统计量,最大值是第n个顺序统计量。而“中位数(median)”总是出现在low((n+1)/2)或者high((n+1)/2)处,其中low是向下取整(“下中位数”),high是向上取整(“上中位数”),当n为奇数的时候,只有“下中位数”,而n为偶数的时候,同时有“下中位数”和“上中位数”。
选择问题的定义如下。
输入:一个包含n个不同的数的集合A和一个数i,i属于范围[1,n]
输出:集合A中的一个元素x,x恰好大于A中的其他i-1个元素
通过排序的方法可以解决这个问题,比如堆排序、归并排序。时间可以达到O(n*lg(n))
下面讨论一个实用的算法,平均情况下运行时间为O(n)
此程序利用了快速排序的partition子程序(随机选择pivot的版本),因为partition总是把比pivot小的划分到左边,比pivot大的划分到右边,所以利用这一点(但是randomizedSelect不会向快速排序一样递归地处理划分出来的两边,而是只处理左边或者右边,因此要更快一些)完成选择。
此算法平均性能比较好,因为是随机化的划分,不会有哪一组特定的输入导致其最坏情况的发生。
平均时间是O(n),最坏时间是O(n^2)
实现代码如下:
package algorithms; import java.util.Arrays;
import java.util.Random;
public class SelectionProblem { //static StringBuilder logger = new StringBuilder(); // debug
//static String NEWLINE = "\n"; // debug /**
* @param a the array
* @param low the lower bound (inclusive)
* @param high the upper bound (exclusive)
* @param i indicate that the i-th order statistic is our target, i starts from 1
* @return the i-th order statistic
* */
public static <T extends Comparable<T>>
T randomizedSelect(T[] a, int low, int high, int i) {
--high; // high the upper bound (exclusive)
return _randomizedSelect(a, low, high, i);
} private static <T extends Comparable<T>>
T _randomizedSelect(T[] a, int low, int high, int i) {
if (low == high) {
return a[low]; // target found
}
// else, partition
int pivot = randomizedPartition(a, low, high);
int k = pivot - low + 1;
if (k == i) { // if pivot is our target
return a[pivot];
} else if (k > i) { // if pivot is too large
return _randomizedSelect(a, low, pivot-1, i);
} else { // if pivot is too small
return _randomizedSelect(a, pivot+1, high, i-k);
}
} private static <T extends Comparable<T>>
int randomizedPartition(T[] a, int low, int high) {
int pivotIndex = randomIndex(low, high+1);
// logger.append("pivotIndex:"+pivotIndex+NEWLINE); // debug
return Partition.doPartition(a, low, high+1, pivotIndex);
} private static final Random random = new Random();
// low (inclusive), high (exclusive)
private static int randomIndex(int low, int high) {
if (high==low) {
return low;
}
return random.nextInt(high-low) + low;
} // test
public static void main(String[] args) {
Integer[] a = new Integer[]{29, 36, 44, 12, 29, 24, 28, 74, 54, 56};
System.out.println(Arrays.toString(a));
Integer result = SelectionProblem.randomizedSelect(a, 0, a.length, 10);
//if (result != 36) { // debug
// System.out.println(logger); // debug
//} // debug
System.out.println("result:"+result);
//System.out.println(Arrays.toString(a)); // debug
QuickSort.sort(a, 0, a.length);
System.out.println(Arrays.toString(a));
} }
Selection Problem (选择问题)的更多相关文章
- the steps that may be taken to solve a feature selection problem:特征选择的步骤
參考:JMLR的paper<an introduction to variable and feature selection> we summarize the steps that m ...
- 选择问题(selection problem)
/* 本文是选择问题: 选择一组N个数当中的第k小的数(第k大的数类似) 集中方法的实现代码 */ #include "sorting.h" #incl ...
- d3.js:数据可视化利器之 selection:选择集
选择集/selection 选择集/selection是d3中的核心对象,用来封装一组从当前HTML文档中选中的元素: d3提供了两个方法用来创建selection对象: select(selecto ...
- unity编辑器扩展_04(使用Selection获取选择的游戏物体)
代码: [MenuItem("Tools/GetChance", false, 1)] static void GetChance() { if (Sel ...
- 选择屏幕(Selection Screen)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现
选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...
- Spark2 Model selection and tuning 模型选择与调优
Model selection模型选择 ML中的一个重要任务是模型选择,或使用数据为给定任务找到最佳的模型或参数. 这也称为调优. 可以对诸如Logistic回归的单独Estimators进行调整,或 ...
- 【排序基础】1、选择排序法 - Selection Sort
文章目录 选择排序法 - Selection Sort 为什么要学习O(n^2)的排序算法? 选择排序算法思想 操作:选择排序代码实现 选择排序法 - Selection Sort 简单记录-bobo ...
- Andrew Ng机器学习公开课笔记 -- Regularization and Model Selection
网易公开课,第10,11课 notes,http://cs229.stanford.edu/notes/cs229-notes5.pdf Model Selection 首先需要解决的问题是,模型 ...
随机推荐
- Codeforces Round #325 (Div. 2) Phillip and Trains dp
原题连接:http://codeforces.com/contest/586/problem/D 题意: 就大家都玩过地铁奔跑这个游戏(我没玩过),然后给你个当前的地铁的状况,让你判断人是否能够出去. ...
- [BZOJ5110]Yazid的新生舞会
题目大意: 给你一个长度为$n(n\leq 5\times 10^5)$的序列$A_{1\sim n}$.求满足区间众数在区间内出现次数严格大于$\lfloor\frac{r-l+1}{2}\rflo ...
- 1.13(java学习笔记)异常机制
异常不同于错误,它是程序运行时产生的未知问题. 如果把程序比喻成一辆汽车,那么汽车开着开着突然前面出现了一个大石头挡住了路,这就叫异常. 那么出现了这个异常我们需要去处理,比如打电话给公路管理局,让它 ...
- jsp笔记3(内置对象)
jsp脚本中的9个内置对象: 1.application:javax.servlet.ServletContext的实例对象,该实例对象代表jsp所属的web应用本身,可用于在jsp页面或Servle ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- Oracle PL/SQL入门之慨述
Oracle PL/SQL入门之慨述 一.PL/SQL出现的目的 结构化查询语言(Structured Query Language,简称SQL)是用来访问关系型数据库一种通用语言,它属于第四代语言( ...
- CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
Xcode 升级到 6.0 后,更新 CocoaPods,出现了如下的警告 [!] The `Paopao [Debug]` target overrides the `PODS_ROOT` buil ...
- 【微信】微信小程序 微信开发工具 创建js文件报错 pages/module/module.js 出现脚本错误或者未正确调用 Page()
创建报错pages/module/module.js 出现脚本错误或者未正确调用 Page() 解决方法: 在js文件中添加 Page({ })
- 【XStream】xml和java实体的相互转化
1.pom.xml <!-- xstream xml和Java对象转化 --> <dependency> <groupId>xstream</groupId& ...
- IMAP 命令
最近学习了一下IMAP命令,现在也算总结一下学习的东西,先说说IMAP命令,如果你使用的是163.126邮箱,反正是网易家的邮箱,那么这里就有很多坑要踩了,因为网易邮箱的特殊性,由于网易邮箱在中国占有 ...