random_select
package sorttest; //expected and worst running time is O(n),asuming that the elements are distinct
import java.util.Random;
public class random_select {
public static int select(int[] b,int lo,int hi,int i){
int[] a = merge.copy(b);
if(lo == hi){
return a[lo];
}
int mid = random_partiton(a,lo,hi);
int rank = mid + 1;
if(i == rank){
return a[mid];
}
else if(i < rank){
return select(a,lo,mid -1,i); //will not null point because i is in the range
}
else {
return select(a,mid + 1,hi,i);
}
}
public static int random_partiton(int[] a,int lo,int hi){
Random random = new Random();
int r = random.nextInt(hi)%(hi-lo+1) + lo; // a random number between lo to hi
exch(a,hi,r);
return quicksort.partition(a, lo, hi);
}
private static void exch(int[] a,int i,int j)
{ int t = a[i]; a[i] = a[j] ; a[j] = t;}
}
最新文章
- maven
- oracle 将多字段数据合成一个
- Screen 对象
- 1046: 最小的K个数
- 【UML】如何看Android的UML图
- ubuntu apache开启重写模块
- BZOJ1520 [POI2006]Szk-Schools
- DaoFactory.java
- DTCMS获取栏目子类
- 一个简单的python选课系统
- Spring第六篇【Spring AOP模块】
- StringBuilder
- ui2code中的深度学习+传统算法应用
- Dijkstra的应用
- u-boot(一)启动简介
- Hadoop---集群的时间同步
- mysql外键的三种关系
- 【apache】phpstudy中apache 隐藏入口文件index.php (解决no input file specified错误)
- PHP 学习笔记之一:thinkPHP的volist标签
- bayer, yuv, RGB转换方法