查找为了k个最大的元素 [英] Find the k largest elements in order

查看:131
本文介绍了查找为了k个最大的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是找到k个最大的一个阵列中的顺序(即从到第k个最大的元素的最大元素开始)的元素最快的方法是什么?

What is the fastest way to find the k largest elements in an array in order (i.e. starting from the largest element to the kth largest element)?

推荐答案

一种办法是以下内容:

  1. 使用线性时间选择算法像中位数的中位数或introsort,找到第k个最大元件和重新排列单元,使得从第k个元素的所有元素向前大于第k个元素

  1. Using a linear-time selection algorithm like median-of-medians or introsort, find the kth largest element and rearrange the elements so that all elements from the kth element forward are greater than the kth element.

排序的所有期待使用像堆排序还是快速排序快速排序算法,从第k个元素。

Sort all elements from the kth forward using a fast sorting algorithm like heapsort or quicksort.

步骤(1)需要时间为O(n),和步骤(2)需要时间为O(K数K)。总体而言,该算法在时间为O(N + K日志k)时,这是非常非常快

Step (1) takes time O(n), and step (2) takes time O(k log k). Overall, the algorithm runs in time O(n + k log k), which is very, very fast.

希望这有助于!

这篇关于查找为了k个最大的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆