Randomized QuickSelect
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.
public int nextInt(int bound)
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的更多相关文章
- Programming Assignment 2: Randomized Queues and Deques
实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...
- Comparing randomized search and grid search for hyperparameter estimation
Comparing randomized search and grid search for hyperparameter estimation Compare randomized search ...
- AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue
RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...
- Programming Assignment 2: Deques and Randomized Queues
编程作业二 作业链接:Deques and Randomized Queues & Checklist 我的代码:Deque.java & RandomizedQueue.java & ...
- 快速排序和快速选择(quickSort and quickSelect)算法
排序算法:快速排序(quicksort)递归与非递归算法 TopK问题:快速选择(quickSelect)算法 import java.util.*; import java.lang.*; publ ...
- 【优化算法】Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解
01 概述 Greedy Randomized Adaptive Search,贪婪随机自适应搜索(GRAS),是组合优化问题中的多起点元启发式算法,在算法的每次迭代中,主要由两个阶段组成:构造(co ...
- Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)
作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...
- Java QuickSelect
Java QuickSelect /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternationa ...
- 排序算法五:随机化快速排序(Randomized quicksort)
上一篇提到,快速排序的平均时间复杂度是O(nlgn),比其他相同时间复杂度的堆排序.归并排序都要快,但这是有前提的,就是假定要排序的序列是随机分布的,而不是有序的.实际上,对于已经排好的序列,如果用快 ...
随机推荐
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- DirectX 开发环境配置
想使用DirectX开发游戏,前提是要搭建DirectX的开发环境啦. 下面我们就一起学习下DirectX开发环境搭建 1. 首先是DirectX SDK安装, 下载地址是: http://www ...
- pyqt 配置文件例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys,datetime from PyQt4.QtC ...
- Binarized Neural Networks_ Training Neural Networks with Weights and Activations Constrained to +1 or −1
转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6248953.html by 少侠阿朱
- unity3d 建树篇
今天碰到有人问这个问题,然后我经过一番折腾,找到了方法.例如以下: 有学过Unity3d的同学生都知道我们在对地形拖拉树木等表层时,其树木在我们实例执行中,它们都是能够任其他物体穿过. 这是为什么.相 ...
- linux svn安装和配置
linux svn安装和配置,不结合apache http://blog.51yip.com/server/901.html 张映 发表于 2010-07-07 分类目录: 服务器相关 今天有个同事在 ...
- Boost线程库学习笔记
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
- 前端--关于javascript对象
在javascript中对象是一种基本的数据类型,在数据结构上是一种散列表,可以看作是属性的无序集合,除了原始值其他一切都是对象.它可以用来表示现实世界中或者我们大脑中抽象出来的客体,这和其他面向对象 ...
- 前端判断用户请求是PC还是移动端
链接:https://www.zhihu.com/question/20004700/answer/13678113 第一步先在服务器端使用User Agent判断,先匹配出移动设备,这一步可以统计U ...