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;}

}

最新文章

  1. maven
  2. oracle 将多字段数据合成一个
  3. Screen 对象
  4. 1046: 最小的K个数
  5. 【UML】如何看Android的UML图
  6. ubuntu apache开启重写模块
  7. BZOJ1520 [POI2006]Szk-Schools
  8. DaoFactory.java
  9. DTCMS获取栏目子类
  10. 一个简单的python选课系统
  11. Spring第六篇【Spring AOP模块】
  12. StringBuilder
  13. ui2code中的深度学习+传统算法应用
  14. Dijkstra的应用
  15. u-boot(一)启动简介
  16. Hadoop---集群的时间同步
  17. mysql外键的三种关系
  18. 【apache】phpstudy中apache 隐藏入口文件index.php (解决no input file specified错误)
  19. PHP 学习笔记之一:thinkPHP的volist标签
  20. bayer, yuv, RGB转换方法

热门文章

  1. 【MySQL】【1】表中存在重复记录,删除保留其中一条
  2. ubuntu 远程登录服务器和服务器中下载代码
  3. python中RabbitMQ的使用(工作队列)
  4. Hive QL的操作
  5. npm使用国内淘宝镜像的方法
  6. Oracle awr报告生成操作步骤
  7. 使用Redis数据库(1)(三十三)
  8. eclipse安装scala环境
  9. 五笔xu
  10. suffix word ard ar arian arium atic ation atory ator out ~3