当前位置: 首页 > news >正文

招聘网站做两份简历/少儿编程培训机构排名前十

招聘网站做两份简历,少儿编程培训机构排名前十,婚庆行业网站建设方案1,小型教育网站的开发建设论文更多代码和Leetcode题目解析请看这里什么是Quick select?Quick select算法通常用来在未排序的数组中寻找第k小/第k大的元素。其方法类似于Quick sort。Quick select和Quick sort都是由Tony Hoare发明的,因此Quick select算法也被称为是Hoares selection algorithm。…

更多代码和Leetcode题目解析请看这里

什么是Quick select?

Quick select算法通常用来在未排序的数组中寻找第k小/第k大的元素。其方法类似于Quick sort。Quick select和Quick sort都是由Tony Hoare发明的,因此Quick select算法也被称为是Hoare's selection algorithm。

Quick select算法因其高效和良好的average case时间复杂度而被广为应用。Quick select的average case时间复杂度为O(n),然而其worst case时间复杂度为O(n^2)。

总体而言,Quick select采用和Quick sort类似的步骤。首先选定一个pivot,然后根据每个数字与该pivot的大小关系将整个数组分为两部分。那么与Quick sort不同的是,Quick select只考虑所寻找的目标所在的那一部分子数组,而非像Quick sort一样分别再对两边进行分割。正是因为如此,Quick select将平均时间复杂度从O(nlogn)降到了O(n)。

Quick select 算法描述

Input: array nums, int k. (find kth smallest element in an unsorted array)

Output: int target

Choose an element from the array as pivot, exchange the position of pivot and number at the end of the array.

_The pivot can either be the end element or a random chosen element. A random chosen pivot can make the algorithm much possibly run in average case time. _

Partition the array into 2 parts in which the numbers in left subarray is less than (or equal to) the pivot and the numbers in right subarray is greater than (or equal to) the pivot.

Exchange pivot (at the end of the array now) with the first element in the right part.

Compare k with length of the left subarray, say, len.

if k == len + 1, then pivot is the target.

if k <= len, repeat from step 1 on the left subarray.

if k > len, k = k - len, repeat from step 1 on the right subarray.

Quick Select Java实现

Java代码如下:

public class Solution {

public int findKthSmallest(int[] nums, int k) {

if (nums == null || nums.length == 0) {

return Integer.MIN_VALUE;

}

int len = nums.length;

return quickSelect(nums, k, 0, len - 1);

}

private int quickSelect(int[] nums, int k, int start, int end) {

//Choose a pivot randomly

Random rand = new Random();

int index = rand.nextInt(end - start + 1) + start;

int pivot = nums[index];

swap(nums, index, end);

int left = start, right = end;

while(left < right) {

if (nums[left++] >= pivot) {

swap(nums, --left, --right);

}

}

//left is now pointing to the first number that is greater than or equal to the pivot

swap(nums, left, end);

//m is the number of numbers that is smaller than pivot

int m = left - start;

if (m == k - 1) { //in order to find the kth smallest number, there must be k - 1 number smaller than it

return pivot;

}

else if (k <= m) { //target is in the left subarray

return quickSelect(nums, k, start, left - 1);

}

else {

//target is in the right subarray, but need to update k

//Since we have discarded m numbers smaller than it which is in the right subarray

return quickSelect(nums, k - m, left, end);

}

}

private void swap(int[] nums, int i, int j) {

int tmp = nums[i];

nums[i] = nums[j];

nums[j] = tmp;

}

}

注意:在上节和本节中给出的算法和代码无法处理数组中有重复的情况。对于数组中有重复数字的情况,可以先扫描整个数组,利用HashSet或其他数据结构得到所有唯一的数字,然后使用本算法处理。

Quick Select 复杂度分析

时间复杂度

完整的平均时间复杂度分析非常复杂,在这里不再赘述。有兴趣的可以看这里。

一般来说,因为我们才用了随机选取pivot的过程,平均来看,我们可以假设每次pivot都能选在中间位置。设算法时间复杂度为T(n)。在第一层循环中,我们将pivot与n个元素进行了比较,这个时间为cn 。

所以第一步时间为:T(n) = cnc + T(n/2)。其中T(n/2)为接下来递归搜索其中一半的子数组所需要的时间。

在递归过程中,我们可以得到如下公式:

T(n/2) = c(n/2) + T(n/4)

T(n/4) = c(n/4) + T(n/8)

...

T(2) = 2*c + T(1)

T(1) = 1*c

将以上公式循环相加消除T项可以得到:

T(n) = c(n + n/2 + n/4 + n/8 + ... + 2 + 1) = 2n = O(n)

因此得到Quick Select算法的时间复杂度为O(n)。

空间复杂度

算法没有使用额外空间,swap操作是inplace操作,所以算法的空间复杂度为O(1)。

http://www.jmfq.cn/news/4895677.html

相关文章:

  • 网站建设培训龙岗/外贸网络推广公司
  • 网站建设费用模板/武汉网站推广排名
  • 企业网站管理的含义及内容/成都网络营销
  • 科协网站页建设的意义/旺道网站优化
  • 网站建设验收意见/电商网站开发平台有哪些
  • 做兼职的设计网站有哪些/惠州seo网站排名
  • 伴奏在线制作网站/天津seo优化排名
  • 宣传广告牌图片/官网关键词优化价格
  • 上海装修公司网站建设/橘子seo查询
  • 重庆找工作哪个网站好/巨量关键词搜索查询
  • wordpress 扫描工具/上海aso优化公司
  • 电子商务网站建设计划书/建网站建设
  • 网站前置审批怎么做/网络营销推广合作
  • 众筹网站建设/关键词歌词打印
  • 西部网站管理助手/百度认证证书
  • 网站中怎么插入flash/sem广告投放是做什么的
  • 做货代哪个网站上好找客户/百度一下 你知道首页
  • 注册门户网站/网站优化排名软件
  • 深圳 旅游 网站建设/网络营销收获与体会
  • 做企业展示版网站贵吗/做百度关键词排名的公司
  • 做网站域名后缀选择/电商平台运营方案思路
  • 做网络私活的网站/核心关键词如何优化
  • 如何写代码做网站/营业推广方案
  • 网站建设合同标准版/站长之家音效
  • 整站seo哪家服务好/推广平台 赚佣金
  • 做程序开发的网站/百度seo学院
  • html5购物网站模板/搭建网站工具
  • 台州做网站是什么/关于seo如何优化
  • 做职业规划的网站/免费获客软件
  • 郑州网站南京网站建设/免费建站网站大全