这里是我使用的是独立库的解决办法, 将在具有阵列的任何编程语言的工作:
初始化:
Make an array of 100 elements and initialise all elements
with a low value (less than any value in your input list).
Initialise an integer variable to 0 (or any value in
[0;99]), say index_minvalue, that will point to the
current lowest value in the array.
Initialise a variable, say minvalue, to hold the current
lowest value in the array.
对于每个值,例如current_value,在输入列表中:
if current_value > minvalue
Replace value in array pointed to by index_minvalue
with current_value
Find new lowest value in the array and set index_minvalue to
its array index. (linear search for this will be OK as the array
is quickly filled up with large values)
Set minvalue to current_value
else
<don't do anything!>
minvalue wil我很快就会得到一个很高的值,因此输入列表中的大部分值 只需要与最小值 比较(比较的结果大部分是错误的)。
你走了。我只是想提出一个优先级队列将是一个很好的方法来处理这与我所建议的算法相结合。不是一个Python程序员,我没有意识到它已经可用。 – tvanfosson 2009-08-02 13:58:24