1. The i th order statistic of a set of n elements is the i th smallest element. Regardless of the parity of n, medians occur at i = ⌊(n + 1)/2⌋ (the lower median) and i = ⌈(n+1)/2⌉ (the upper median). Here, we consistently use the phrase “the median” to refer to the lower median.
2. We formally specify the selection problem as follows:
Input: A set A of n (distinct) numbers and an integer i , with 1 ≤ i ≤ n.
Output: The element x ∈ A that is larger than exactly i-1 other elements of A.
3. we can find both the minimum and the maximum using at most 3⌊n/2⌋
comparisons. We do so by maintaining both the minimum and maximum elements
seen thus far. We compare pairs of elements from the input first with each other, and then we compare the smaller with the current minimum and the larger to the current maximum, at a cost of 3 comparisons for every 2 elements. How we set up initial values for the current minimum and maximum depends on whether n is odd or even. If n is odd, we set both the minimum and maximum to the value of the first element, and then we process the rest of the elements in pairs. Then we perform 3⌊n/2⌋ comparisons. If n is even, we perform 1 comparison on the first 2 elements to determine the initial values of the minimum and maximum, and then process the rest of the elements in pairs as in the case for odd n. We perform 1 initial comparison followed by 3(n - 2)/2 comparisons, for a total of 3n/2-2. Thus, in either case, the total number of comparisons is at most 3⌊n/2⌋.
4. As in quicksort, randomSelect partition the input array recursively. But unlike quicksort, which recursively processes both sides of the partition, it works on only one side of the partition. Quicksort has an expected running time of θ(n lg n), the expected running time of randomSelect is θ(n), assuming that the elements are distinct:
int randomSelect(int[] arr, int start, int end, int i ) {
// if the array only has one element
if (start==end) return arr[start];
int q = randomPartition(arr, start, end);
int k = q - start + 1;
// if pivot is the answer
if ( k == i ) return arr[q];
// if i < k , then the answer is the ith smallest one in left subarray
if ( i < k ) return randomSelect(arr, start, q - 1, i);
// if i > k , then the answer is the i-k th smallest one in right subarray
else return randomSelect(arr, q + 1, end , i - k );
}
The worst-case running time for randomSelect is θ(n^2), even to find the minimum, because we could be extremely unlucky and always partition around
the largest remaining element, and partitioning takes θ(n) time. However, randomSelect can find any order statistic, and in particular the median, in
expected linear time, assuming that the elements are distinct.
5. The algorithm select finds the desired element in O(n) time in worst case by recursively partitioning the input array and guaranteeing a good split upon partitioning the array. The select algorithm determines the i th smallest of an input array of n > 1 distinct elements by executing the following steps. (If n = 1, then select merely returns its only input value as the i th smallest.)
1) Divide the n elements of the input array into ⌊n/5⌋ groups of 5 elements each and at most one group made up of the remaining n mod 5 elements.
2) Find the median of each of the ⌊n/5⌋ groups by first insertion-sorting the elements of each group (of which there are at most 5) and then picking the median from the sorted list of group elements.
3) Use select recursively to find the median x of the ⌊n/5⌋ medians found in
step 2). (If there are an even number of medians, then x is the lower median.)
4) Partition the input array around the median-of-medians x. Let k be one more than the number of elements on the low side of the partition, so that x is the kth smallest element and there are n-k elements on the high side of the partition.
5) If i = k, then return x. Otherwise, use select recursively to find the i th
smallest element on the low side if i < k, or the (i-k)th smallest element on
the high side if i > k.
At least half of the medians found in step 2) are greater than or equal to the median-of-medians x.Thus, at least half of the ⌊n/5⌋ groups contribute at least 3 elements that are greater than x, except for the one group that has fewer than 5 elements if 5 does not divide n exactly, and the one group containing x itself. Discounting these two groups, it follows that the number of elements greater than x is at least 3n/10 - 6. Similarly, at least 3n/10 - 6 elements are less than x. Thus, in the worst case, step 5) calls select recursively on at most 7n/10 + 6 elements.
6. Implementation:
public class MedianOfMediansSelector { private int[] arr; public MedianOfMediansSelector( int[] arr) { this.arr = arr; } public int select(int index) { if (index < 0 && index >= arr.length) throw new IllegalArgumentException("Index out of boundary"); int end = arr.length; int start = 0; int pivotIndex = choosePivot(start, end); pivotIndex = partition(start, end, pivotIndex); while (pivotIndex != index) { if( pivotIndex > index ) { end = pivotIndex; pivotIndex = choosePivot(start, end); pivotIndex = partition(start, end, pivotIndex); } else { start = pivotIndex + 1; pivotIndex = chooser.choosePivot(start, end); pivotIndex = partition(start, end, pivotIndex); } } return arr[index]; } private int partition(int start, int end , int pivotIndex){ //swap with the partition element with the first one int temp = arr[start]; arr[start] = arr[pivotIndex]; arr[pivotIndex] = temp; int pivot = arr[start]; int i=start+1,j=end-1; while( i <= j ) { while ( i <= j && arr[i] <= pivot ) i ++; if (i > j) break; while (arr[j] > pivot ) j--; if ( i > j) break; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } i--; arr[start] = arr[i]; arr[i] = pivot; return i; } private int choosePivot(int start, int end) { if ( start >= end ) throw new IllegalArgumentException("end should bigger than start!"); if ( end > arr.length ) throw new IllegalArgumentException("end should not bigger than arry length!"); if ( end - start <= 2) { return start; } // sort elements in each block int BLOCK_SIZE = 5; int i = BLOCK_SIZE; int num = 1; for ( ; i < end ; i += BLOCK_SIZE , num ++) { insertionSort(i - BLOCK_SIZE, i); } i -= BLOCK_SIZE; insertionSort(i, end); // choose medium of each group int medium = BLOCK_SIZE/2; int[] mediums = new int[num]; int r = ( i + end ) / 2; // medium item in last block i = 0 ; for ( int j = medium; i < num - 1 ; j += BLOCK_SIZE, i ++ ) { mediums[i] = arr[j]; } mediums[i] = arr[r]; MedianOfMediansSelector selector = new MedianOfMediansSelector (mediums); int pivot = selector.select(num/2); for ( i = start; i < end; i ++) { if ( arr[i] == pivot) return i; } throw new IllegalStateException("Medium of Medium didn't find a correct pivot!"); } private void insertionSort(int start, int end) { int temp; for ( int i = start + 1; i < end ; i ++ ) { for ( int j = i ; j > 0 ; j --) { if ( arr[j] < arr[j-1] ) { temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } else { break; } } } } }
相关推荐
修炼成Javascript中级程序员必知必会_资源分享
内容概要:本文详细介绍了如何使用MATLAB的深度学习工具箱,在果树病虫害识别任务中从数据准备、模型设计、训练优化到最后的模型评估与应用全流程的具体实施步骤和技术要点。涵盖了MATLAB深度学习工具箱的基本概念及其提供的多种功能组件,如卷积神经网络(CNN)的应用实例。此外,文中还具体讲述了数据集的收集与预处理方法、不同类型的深度学习模型搭建、训练过程中的超参数设定及其优化手段,并提供了病虫害识别的实际案例。最后展望了深度学习技术在未来农业领域的潜在影响力和发展前景。 适合人群:对深度学习及农业应用感兴趣的科研人员、高校师生和相关从业者。 使用场景及目标:①希望掌握MATLAB环境下构建深度学习模型的方法和技术细节;②从事果树病虫害管理研究或实践,寻找高效的自动化解决方案。 阅读建议:在阅读本文之前,建议读者熟悉基本的MATLAB编程环境及初步了解机器学习的相关概念。针对文中涉及的理论和技术难点,可以通过官方文档或其他教程进行补充学习。同时,建议动手实践每一个关键点的内容,在实践中加深理解和掌握技能。
nodejs010-nodejs-block-stream-0.0.7-1.el6.centos.alt.noarch.rpm
机械模型与技术交底书的融合:创新点详解与解析,机械模型加技术交底书,有创新点 ,机械模型; 技术交底书; 创新点,创新机械模型与技术交底书详解
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
nodejs010-nodejs-cmd-shim-1.1.0-4.1.el6.centos.alt.noarch.rpm
西门子四轴卧加后处理系统:828D至840D兼容,四轴联动高效加工解决方案,支持图档处理及试看程序。,西门子四轴卧加后处理,支持828D~840D系统,支持四轴联动,可制制,看清楚联系,可提供图档处理试看程序 ,核心关键词:西门子四轴卧加后处理; 828D~840D系统支持; 四轴联动; 制程; 联系; 图档处理试看程序。,西门子四轴卧加后处理程序,支持多种系统与四轴联动
基于黏菌优化算法(SMA)的改进与复现——融合EO算法更新策略的ESMA项目报告,黏菌优化算法(SMA)复现(融合EO算法改进更新策略)——ESMA。 复现内容包括:改进算法实现、23个基准测试函数、多次实验运行并计算均值标准差等统计量、与SMA对比等。 程序基本上每一步都有注释,非常易懂,代码质量极高,便于新手学习和理解。 ,SMA复现;EO算法改进;算法实现;基准测试函数;实验运行;统计量;SMA对比;程序注释;代码质量;学习理解。,标题:ESMA算法复现:黏菌优化与EO算法融合改进的实证研究
基于MATLAB的Stewart平台并联机器人仿真技术研究与实现:Simscape环境下的虚拟模拟分析与应用,MATLAB并联机器人Stewart平台仿真simscape ,MATLAB; 并联机器人; Stewart平台; 仿真; Simscape; 关键技术。,MATLAB中Stewart平台并联机器人Simscape仿真
Grad-CAM可视化医学3D影像
探索comsol泰勒锥:电流体动力学的微观世界之旅,comsol泰勒锥、电流体动力学 ,comsol泰勒锥; 电流体动力学; 锥形结构; 电场影响,COMSOL泰勒锥与电流体动力学研究
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
PFC6.03D模型动态压缩模拟与SHPB霍普金森压杆系统理论及实验数据处理技术解析,PFC6.03D模型,动态压缩模拟,还包括: SHPB霍普金森压杆系统理论知识介绍,二波法和三波法处理实验数据,提出三波波形,计算动态压缩强度等 ,PFC模型; 动态压缩模拟; SHPB霍普金森压杆系统; 理论介绍; 二波法处理; 三波法处理; 三波波形; 动态压缩强度。,"PFC模型下的动态压缩模拟及SHPB理论实践研究"
ProASCI 开发板原理图,适用于A3P3000
免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
1、文件内容:pykde4-devel-4.10.5-6.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/pykde4-devel-4.10.5-6.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
基于Comsol模拟的三层顶板随机裂隙浆液扩散模型:考虑重力影响的瞬态扩散规律分析,Comsol模拟,考虑三层顶板包含随机裂隙的浆液扩散模型,考虑浆液重力的影响,模型采用的DFN插件建立随机裂隙,采用达西定律模块中的储水模型为控制方程,分析不同注浆压力条件下的浆液扩散规律,建立瞬态模型 ,Comsol模拟; 随机裂隙浆液扩散模型; 浆液重力影响; DFN插件; 达西定律模块储水模型; 注浆压力条件; 浆液扩散规律; 瞬态模型,Comsol浆液扩散模型:随机裂隙下考虑重力的瞬态扩散分析
A simple fast, easy use distributed file system written by golang(similar fastdfs).go-fastdfs
手机编程-1738391552157.jpg