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 #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】
B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- [POI2013]Tower Defense Game
题目大意: 一个$n(n\le5\times10^5)$个点$m(m\le10^6)$条边的无向图,边权全为$1$,满足若一个标记点能覆盖与其距离不超过$1$的点,从中选取不超过$k$个点能将整张图覆 ...
- 解决Eclipse 变量名的自动补全问题
大家使用eclipse敲代码的时候,是不是都被这样一个问题困扰着.就是键入一个变量名的时候,会自动提示补全:在你的变量名后面加上类型的名字!这个时候,你就必须键入Esc才不会自动补全你的变量,如果你键 ...
- C/C++框架和库 (真的很强大) 转
http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 - 1. Webbench Webbench ...
- NAND_FLASH_内存详解与读写寻址方式
一.内存详解 NAND闪存阵列分为一系列128kB的区块(block),这些区块是 NAND器件中最小的可擦除实体.擦除一个区块就是把所有的位(bit)设置为"1"(而所有字节(b ...
- [Android Traffic] 使用缓存来避免重复的下载
转载自: http://blog.csdn.net/kesenhoo/article/details/7395817 Redundant Downloads are Redundant[重复下载是冗余 ...
- 用new和delete运算符进行动态分配和撤销存储空间
測试描写叙述:暂时开辟一个存储空间以存放一个结构体数据 #include <iostream> #include <string> using namespace std; s ...
- tez参数
https://tez.apache.org/releases/0.8.4/tez-api-javadocs/configs/TezConfiguration.html
- 格式化HDFS
格式化HDFS 查看hdfs-site.xml 将 dfs.namenode.name.dir和dfs.datanode.data.dir 目录中文件删除 <configuration> ...
- crontab配置
1.命令功能 通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔的单位可以是分钟.小时.日.月.周及以上的任意组合.这个命令非常适合周期性的 ...