博客
关于我
微软高频面试模拟题: 数组中第K大的元素:快速选择算法
阅读量:230 次
发布时间:2019-03-01

本文共 410 字,大约阅读时间需要 1 分钟。

快速找到第k大的数的算法

在这个问题中,我们需要找到数组中的第k大的数。传统的方法是使用快速排序来减少复杂度,这种方法的时间复杂度为O(n),因为它大约只需要常数次操作就能找到答案。

思路如下:首先选取数组中的第一个数作为基准,然后通过一次快速排序操作将其放到正确的位置。如果这个基准正好是距离右端点的第k个数,那么它就是我们要找的数。如果它距离右端点的位置比k大,则说明要找的数在基准的右边;如果距离右端点的位置比k小,则说明要找的数在基准的左边。

具体来说,我们通过递归的方式对数组进行操作。首先确定基准的位置,然后根据基准的位置和数组的长度来决定下一步的查找方向。这种方法的核心在于每次操作都尽可能地减少需要检查的范围,从而快速缩小搜索范围。

这种方法的时间复杂度为O(n),因为它每次操作都能大幅减少问题规模,避免了传统的O(n^2)复杂度。这种递归的方式类似于快速排序,其核心思想是通过分治策略来高效解决问题。

转载地址:http://ojqv.baihongyu.com/

你可能感兴趣的文章
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>