[Tutor] max min value in array
Kent Johnson
kent37 at tds.net
Thu Sep 17 14:34:23 CEST 2009
On Thu, Sep 17, 2009 at 8:06 AM, Rich Lovely <roadierich at googlemail.com> wrote:
> 2009/9/17 Rayon <evosweet at hotmail.com>:
>> I need to find the max and min value from some floats in a array:
> Depending on the size of the array, there's two methods: Using the
> max and min builtin functions, which will be faster for small arrays,
> or hand-coding a single pass function, which might be faster for
> arrays above a certain size.
Why do you expect a hand-coded function to scale better? I would
expect them both to be O(n).
> def minmax2(a):
> #This only takes one pass, but is python, which is not as fast as C code.
> #This also has the advantage of allowing you to easily customize
> your lookups and comparisons
min() and max() both take key= parameters allowing you to customize
the comparison.
> Other techniques to consider: If order is not important, take the
> first and last values after sorting (or after sorting a copy).
Sorting is O(n log n) so this will have relatively worse performance
with larger arrays.
Kent
More information about the Tutor
mailing list