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. c# aes 加密解密

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. 【剑指offer】面试题32:从1到n整数中1出现的次数

    题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...

  3. 关于Redis

    在同步dump.rdb文件时要执行service redis stop后,再拷贝目标rdb文件过去,然后再start 而不是拷贝目标rdb文件过去后直接执行restart  因为redis在执行sto ...

  4. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  5. 自定义一个searchBar

    #import "CZSearchBar.h" @implementation CZSearchBar - (instancetype)initWithFrame:(CGRect) ...

  6. Lua Table 操作

    Lua中table类似与C#种的字典,其实就是一个key-value键值对数据结构.来学习下table基本操作 Table的创建 myTable = {} --表名后面使用{}赋值,表示一个空的表 m ...

  7. textChanged(*)重点

    # -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...

  8. 判断一个key 是否在map中存在

    public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...

  9. 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略

    水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可:   .parent { text-align:center; } 水 ...

  10. SQL内连接-外连接join,left join,right join,full join

    1.创建测试表test1及test2 SQL)); 表已创建. SQL)); 表已创建. ,'name1'); ,'name2'); ,'name3'); ,'name4'); ,'name5'); ...