收集尽可能少算法 [英] Collect Lowest Numbers Algorithm

查看:96
本文介绍了收集尽可能少算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种算法(或PHP code,我想),以结束与从一组号码的10个最低的数字。我想使一个10项数组,检查是否当前的数目比阵列中的号码中的一个较低的,如果是的话,发现最高编号的数组中,并与当前的数字替换它。

I'm looking for an algorithm (or PHP code, I suppose) to end up with the 10 lowest numbers from a group of numbers. I was thinking of making a ten item array, checking to see if the current number is lower than one of the numbers in the array, and if so, finding the highest number in the array and replacing it with the current number.

不过,我打算从数千找到最低的10个号码,并在想可能有一个更快的方法来做到这一点。我计划在PHP实现这一点,所以任何PHP函数是可用的。

However, I'm planning on finding the lowest 10 numbers from thousands, and was thinking there might be a faster way to do it. I plan on implementing this in PHP, so any native PHP functions are usable.

推荐答案

您要寻找的被称为选择算法。关于这一主题的维基百科页面有几个小节的选择的 K 的最小或最大的元素部分。当列表足够大,你可以的击败的所需天真的时间排序的整个列表,然后选择第一个10的算法。

What you're looking for is called a selection algorithm. The Wikipedia page on the subject has a few subsections in the selecting k smallest or largest elements section. When the list is large enough, you can beat the time required for the naive "sort the whole list and choose the first 10" algorithm.

这篇关于收集尽可能少算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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