quickselect算法
来源:互联网 发布:广电网络用什么机顶盒 编辑:程序博客网 时间:2024/06/05 14:57
quickselect算法是从一个无序数组里选择出第k小的元素,它和快速排序算法很像,因为作者都是同一个人==。该算法的时间复杂度为O(n)情况最差的时候时间复杂度为O(n2)
快速选择的算法使用上述算法,寻找k次就可以了
快速选择算法借鉴快速排序算法,快速排序的算法如下:
function partition(list,left,right,pivotIndex): pivotValue := list[pivotIndex] swap list[pivotIndex] and list[right] // Move pivot to end storeIndex := left for i from left to right-1 if list[i] < pivotValue swap list[storeIndex] and list[i] increment storeIndex swap list[right] and list[storeIndex] // Move pivot to its final place return storeIndex上述算法是在数组中将pivotIndex位置处的值插入到left到right之间的适当位置,使storeIndex之前的值都比他小,之后的值都比他大。
快速选择的算法使用上述算法,寻找k次就可以了
function select(list, left, right, k) if left = right return list[left] pivotIndex := ... //随机一个数 pivotIndex := partition(list, left, right, pivotIndex if k = pivotIndex return list[k] else if k < pivotIndex return select(list, left, pivotIndex - 1, k) else return select(list, pivotIndex + 1, right, k)
0 0
- quickselect算法
- quickselect
- QuickSelect
- O(n)的quickselect算法
- Reservoir Sampling & Quickselect
- 快速选择排序(quickselect)--基于quicksort
- 快速选择(QuickSelect)的平均时间复杂度分析
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- mp4文件格式简明教程-mp4文件格式通俗解释
- linux 远程连接oracle 安装instantclient简易客户端
- 词法分析器的实现
- OpenCV中阈值操作
- org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupportedWARNING: Request met
- quickselect算法
- 【备忘】Dubbo与Zookeeper视频教程分布式架构+第三方支付
- android卡片式布局
- ZCA白化(协方差矩阵变换成单位矩阵)和数据零均值处理
- Twemproxy代理Redis-cluster
- sed 匹配最短模式的方法
- 前端代码规范V1.0
- iOS---Xcode中删除描述文件Provisioning Profiles
- 结构体数组 循环显示成员