In this blog, we give a solution for Quick Select.

Here, we have an improvement. The idea is to randomly pick a pivot element.

Main difference is how we implement partition.

Java Random

public int nextInt(int bound)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive).
int idx = new Random().nextInt(nums.length);
int random = (nums[idx]);

Codes

 public class Solution {
private int partition(int[] nums, int start, int end) {
int pivot = nums[end];
int currentSmaller = start - 1;
for (int i = start; i < end; i++) {
if (nums[i] < pivot) {
currentSmaller++;
swap(nums, i, currentSmaller);
}
}
currentSmaller++;
swap(nums, end, currentSmaller);
return currentSmaller;
} public int randomPartition(int[] nums, int start, int end) {
int length = end - start + 1;
int idx = new Random().nextInt(length);
int randomIndex = idx + start;
swap(nums, last, randomIndex);
partition(nums, start, end);
}
}

The assumption in the analysis is, random number generator is equally likely to generate any number in the input range.

The worst case time complexity of the above solution is still O(n2). In worst case, the randomized function may always pick a corner element. The expected time complexity of above randomized QuickSelect is Θ(n), see CLRS book or MIT video lecture for proof.

最新文章

  1. PHP性状的使用
  2. JavaI/O系统
  3. 多线程socket编程示例
  4. 在Windows下利用MinGW编译FFmpeg
  5. OpenStack,ceph
  6. Lua中点和冒号的区别
  7. Vue.js学习 Item5 -- 计算属性computed与$watch
  8. samba服务设置,Linux系统和Windows文件共享
  9. 从unity3d官网下载教程
  10. [转载]__type_traits
  11. ubuntu下安装fiddler
  12. [Android学习笔记]页面布局
  13. 机器学习基石 3 Types of Learning
  14. 谱聚类(Spectral clustering)分析(1)
  15. IBM芯片新功能:诊断癌症
  16. Core Java 谈谈 ThreadPoolExecutor
  17. Python读取配置文件,并连接数据库SQL Server
  18. Android 框架练成 教你打造高效的图片加载框架
  19. centos的基本操作
  20. socket与http

热门文章

  1. Mysql 创建用户并对其赋予操作权限
  2. 简易的C/S系统(实现两个数的和)
  3. bash的元字符(下)
  4. [转]Laravel 4之Eloquent ORM
  5. Qt导出Excel的简单实现
  6. zabbix server is not running: the information displayed may not be current
  7. css-文本及其他
  8. asp.net 通过js调用webService注意
  9. Android--------解决ScrollView中嵌套ExpandableListView显示不全的问题
  10. vs2010中socket链接错误LINK2019