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.

Randomized QuickSelect的更多相关文章

  1. Programming Assignment 2: Randomized Queues and Deques

    实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...

  2. Comparing randomized search and grid search for hyperparameter estimation

    Comparing randomized search and grid search for hyperparameter estimation Compare randomized search ...

  3. AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue

    RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...

  4. Programming Assignment 2: Deques and Randomized Queues

    编程作业二 作业链接:Deques and Randomized Queues & Checklist 我的代码:Deque.java & RandomizedQueue.java & ...

  5. 快速排序和快速选择(quickSort and quickSelect)算法

    排序算法:快速排序(quicksort)递归与非递归算法 TopK问题:快速选择(quickSelect)算法 import java.util.*; import java.lang.*; publ ...

  6. 【优化算法】Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解

    01 概述 Greedy Randomized Adaptive Search,贪婪随机自适应搜索(GRAS),是组合优化问题中的多起点元启发式算法,在算法的每次迭代中,主要由两个阶段组成:构造(co ...

  7. Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)

    作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...

  8. Java QuickSelect

    Java QuickSelect /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternationa ...

  9. 排序算法五:随机化快速排序(Randomized quicksort)

    上一篇提到,快速排序的平均时间复杂度是O(nlgn),比其他相同时间复杂度的堆排序.归并排序都要快,但这是有前提的,就是假定要排序的序列是随机分布的,而不是有序的.实际上,对于已经排好的序列,如果用快 ...

随机推荐

  1. linux下netstat命令详解

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  2. MCM1988 问题B_lingo_装货问题

    两辆平板车的装货问题有七种规格的包装箱要装到两辆铁路平板车上去包装箱的宽和高是一样的但厚度(t,以厘米计)及重量(,以公斤计)是不同的.下表给出了每种包装箱的厚度重量以及数量每辆平板车有10.2 米长 ...

  3. JS帮你计算属相

        背景:一个人出生在2014年的正月初一,他的生肖到底是属蛇还是属马呢?这就要确定那一天才是一年的开始.是春节还是立春?每年的春节是正月初一,但是生肖必须是从立春日开始计算.春节是1912年孙中 ...

  4. qt画刷和画笔

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #画刷和画笔:QBrush 定义了 QPainter 的填充模式,具 ...

  5. struts2——配置struts.xml文件

    在struts2框架中struts.xml是应当放到src的根目录,程序编译后会将struts.xml放到WEB-INF/classes目录下. Struts2在web.xml中的一般配置如下: &l ...

  6. "git rm" 和 "rm" 的区别

    "git rm" 和 "rm" 的区别 FEB 3RD, 2013 | COMMENTS 这是一个比较肤浅的问题,但对于 git 初学者来说,还是有必要提一下的 ...

  7. Android学习笔记--远程服务的使用

    1.AIDL和Binder Android系统四大组件Activity, Content Provider, Broadcast和Service均可以进行跨进程数据传输. Activity可以隐式调用 ...

  8. 继承之后的使用注意事项_ArrayStoreException

    今天在看Core In Java第五章节时,看到一个很感兴趣的知识点,如下: 在Java中,子类数组的引用可以转换成超类数组的引用,而不需要采用强制转换.但是,在超类数组的引用添加超类类型引用对象之后 ...

  9. python os模块文件相关

    使用前 import os导入模块   os模块: os.sep     可以取代操作系统特定的路径分割符 os.linesep  字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...

  10. MJRefresh(上拉加载下拉刷新)

    整理自:https://github.com/CoderMJLee/MJRefresh#%E6%94%AF%E6%8C%81%E5%93%AA%E4%BA%9B%E6%8E%A7%E4%BB%B6%E ...