在一个由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 (选择问题)的更多相关文章

  1. 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 ...

  2. 选择问题(selection problem)

    /*     本文是选择问题: 选择一组N个数当中的第k小的数(第k大的数类似)     集中方法的实现代码 */       #include "sorting.h" #incl ...

  3. d3.js:数据可视化利器之 selection:选择集

    选择集/selection 选择集/selection是d3中的核心对象,用来封装一组从当前HTML文档中选中的元素: d3提供了两个方法用来创建selection对象: select(selecto ...

  4. unity编辑器扩展_04(使用Selection获取选择的游戏物体)

    代码: [MenuItem("Tools/GetChance", false, 1)]    static void GetChance()    {        if (Sel ...

  5. 选择屏幕(Selection Screen)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现

    选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...

  7. Spark2 Model selection and tuning 模型选择与调优

    Model selection模型选择 ML中的一个重要任务是模型选择,或使用数据为给定任务找到最佳的模型或参数. 这也称为调优. 可以对诸如Logistic回归的单独Estimators进行调整,或 ...

  8. 【排序基础】1、选择排序法 - Selection Sort

    文章目录 选择排序法 - Selection Sort 为什么要学习O(n^2)的排序算法? 选择排序算法思想 操作:选择排序代码实现 选择排序法 - Selection Sort 简单记录-bobo ...

  9. Andrew Ng机器学习公开课笔记 -- Regularization and Model Selection

    网易公开课,第10,11课 notes,http://cs229.stanford.edu/notes/cs229-notes5.pdf   Model Selection 首先需要解决的问题是,模型 ...

随机推荐

  1. oracle数据迁移之Exp和Expdp导出数据的性能对比与优化

    https://wangbinbin0326.github.io/2017/03/31/oracle%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB%E4%B9%8BExp%E ...

  2. highcharts 图例详解

    highcharts 图例 tooltip: {                                           },                      legend: { ...

  3. [POI2014]Solar Panels

    题目大意: $T(T\le1000)$组询问,每次给出$A,B,C,D(A,B,C,D\le10^9)$,求满足$A\le x\le B,C\le y\le D$的最大的$\gcd(x,y)$. 思路 ...

  4. apache 单独生成模块

    apache 单独生成模块 一般这种模块都是后期自己去生成的,比如一般在安装apache时都会--enable-so  ,允许动态加载模块. 这个模块你可以这样去生成. 1.下载一个与当前使用的apa ...

  5. IIS7.5 部署WCF项目问题集锦

    HTTP 错误 500.19 - Internal Server Error 描述:配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (overri ...

  6. ArcMAP中如何将16位保存的卫星底图,转变为8位表示

    首先说明,这种转换将会去除影像的投影像素的定义,并在转换后变为黑色的部分.16位的存储,一方面也是定义透明非数据像素点表示的方便.但是这种定义直接加大了影像的大小,不便于与CAD等软件进行交换数据.

  7. 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';

    后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...

  8. iOS:自定义模态动画 --UIPresentationController

    UIPresentationController :展示控制器,是iOS8的一个新特性,用来展示模态窗口的.它是所有模态控制器的管理者. 即: 1> 管理所有Modal出来的控制器 2>  ...

  9. 通过脚本发送zabbix微信报警

    实现zabbix通过微信报警的方式也是通过脚本来实现,与邮件报警不同的是,脚本调用的微信的相关接口的获取相对复杂一点 1.申请一个微信公众号(企业号) 申请方法不多说,如果已申请请忽略 2.在微信企业 ...

  10. redis中的事务(版本2.6.16)

    一.命令支持 1.multi 开始事务 2.exec事务提交 3.取消事务discard 二.事务示例 1.示例 redis>set key1 20OKredis>mutilOKredis ...