在一个由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. 多模态检索之CCA算法

    多模态检索主要是实现不同模态下的数据能相互检索,例如文本模态数据,和图像模态数据.要能实现他们之间的相互检索,首先要是它们相互关联起来.CCA·算法用于多模态检索步骤:      1)首先提取文本,图 ...

  2. 细说JavaScript对象(3):hasOwnProperty

    判断一个属性是定义在对象本身而不是继承自原型链,我们需要使用从 Object.prototype 继承而来的 hasOwnProperty 方法. hasOwnProperty 方法是 JavaScr ...

  3. mybatis-mysql小优化

    原文:http://blog.csdn.net/jinzhencs/article/details/51656548 1.查询某条记录是否存在 <!-- 查询s是否被创建过:Uuid,name, ...

  4. 高并发下的Node.js与负载均衡

    新兴的Node.js已经吸引了很多开发人员的眼光,它提供给我们一个快速构建高性能的网络应用的平台.我也开始逐步投入node.js的怀抱,在学习和使用的过程中,遇到了一些问题,也有一些经验,我觉得有必要 ...

  5. android intent 跳转

    转自:http://blog.sina.com.cn/s/blog_7309444701014u2d.html 一.不需要返回值的跳转 Intent intent=new Intent(); inte ...

  6. npm中package-lock.json的作用:npm install安装时使用

    简单理解: XYZ 的格式 对应为: 主版本号.次版本号.修订号,版本号递增规则如下: 主版本号:当你做了不兼容的 API 修改, 次版本号:当你做了向下兼容的功能性新增, 修订号:当你做了向下兼容的 ...

  7. 摄像头bug查找工作总结

    近期花了很长时间在libcamera中查找和解决一个bug.下面将这段时间中的工作过程,以及对camera的认识总结如下: 首先是问题的发生,在UM2801中,摄像头的代码已经基本实现,并且相应功能也 ...

  8. CvArr、Mat、CvMat、IplImage、BYTE转换

    一.Mat类型:矩阵类型,Matrix.     在openCV中.Mat是一个多维的密集数据数组. 能够用来处理向量和矩阵.图像.直方图等等常见的多维数据.     Mat有3个重要的方法:     ...

  9. SQL Script for read information from a csv file in FTP Server

    DECLARE w_file_path VARCHAR2(4000) := 'XXIF_INPUT'; --all_directories.directory_name w_file_name VAR ...

  10. sql取随机结果集

    应用场景: 某日,接产品姐姐需求,网站搜索页在搜索特定的内容时候,会现实搜索不到结果!如衣服网站,搜索鞋子等.为了不直接呈现一个赤裸裸的无此商品页面,so,需要在搜索商品件数小于3时,在下面随机推荐本 ...