What is the best way to get the minimum or maximum value from an Array of numbers?

后端 未结 18 833
日久生厌
日久生厌 2020-11-27 03:00

Let\'s say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2]

What is the best way to find the minimum or maximum value in that Array?

Right now,

相关标签:
18条回答
  • 2020-11-27 03:54

    Amazed no-one mentioned parallelism here.

    If you got really a huge array, you can use parallel-for, on sub ranges. In the end compare all sub-ranges. But parallelism comes width some penalty too, so this would not optimize on small arrays. However if you got huge datasets it starts to make sense, and you get a time division reduction nearing the amount of threads performing the test.

    0 讨论(0)
  • 2020-11-27 03:56

    The theoretical answers from everyone else are all neat, but let's be pragmatic. ActionScript provides the tools you need so that you don't even have to write a loop in this case!

    First, note that Math.min() and Math.max() can take any number of arguments. Also, it's important to understand the apply() method available to Function objects. It allows you to pass arguments to the function using an Array. Let's take advantage of both:

    var myArray:Array = [2,3,3,4,2,2,5,6,7,2];
    var maxValue:Number = Math.max.apply(null, myArray);
    var minValue:Number = Math.min.apply(null, myArray);
    

    Here's the best part: the "loop" is actually run using native code (inside Flash Player), so it's faster than searching for the minimum or maximum value using a pure ActionScript loop.

    0 讨论(0)
  • 2020-11-27 03:58

    Shortest way :

    Math.min.apply(null,array); //this will return min value from array
    Math.max.apply(null,array); //this will return max value from array

    otherway of getting min & max value from array

     function maxVal(givenArray):Number
        {
        var max = givenArray[0];
        for (var ma:int = 0; ma<givenArray.length; ma++)
        {
        if (givenArray[ma] > max)
        {
        max = givenArray[ma];
        }
        }
        return max;
        }
    
        function minVal(givenArray):Number
        {
        var min = givenArray[0];
        for (var mi:int = 0; mi<givenArray.length; mi++)
        {
        if (givenArray[mi] < min)
        {
        min = givenArray[mi];
        }
        }
        return min;
        }
    

    As you can see, the code in both of these functions is very similar. The function sets a variable - max (or min) and then runs through the array with a loop, checking each next element. If the next element is higher than the current, set it to max (or min). In the end, return the number.

    0 讨论(0)
  • 2020-11-27 04:00

    This depends on real world application requirements.

    If your question is merely hypothetical, then the basics have already been explained. It is a typical search vs. sort problem. It has already been mentioned that algorithmically you are not going to achieve better than O(n) for that case.

    However, if you are looking at practical use, things get more interesting. You would then need to consider how large the array is, and the processes involved in adding and removing from the data set. In these cases, it can be best to take the computational 'hit' at insertion / removal time by sorting on the fly. Insertions into a pre-sorted array are not that expensive.

    The quickest query response to the Min Max request will always be from a sorted array, because as others have mentioned, you simply take the first or last element - giving you an O(1) cost.

    For a bit more of a technical explanation on the computational costs involved, and Big O notation, check out the Wikipedia article here.

    Nick.

    0 讨论(0)
  • 2020-11-27 04:01

    After reading everyone's comments (thank you for your interest), I found that the "best" way (least amount of code, best performing) to do this was to simply sort the Array, and then grab the first value in the Array:

    var myArray:Array /* of Number */ = [2,3,3,4,2,2,5,6,7,2];
    
    myArray.sort(Array.NUMERIC);
    
    var minValue:int = myArray[0];
    

    This also works for an Array of Objects - you simply use the Array.sortOn() function and specify a property:

    // Sample data
    var myArray:Array /* of XML */ = 
        [
        <item level="2" name="a" />
        <item level="3" name="b" />
        <item level="3" name="c" />
        <item level="2" name="d" />
        <item level="5" name="e" />
        ]
    
    // Perform a descending sort on the specified attribute in Array to get the maximum value
    myArray.sortOn("@level", Array.DESCENDING | Array.NUMERIC);
    
    var lowestLevel:int = myArray[0].@level;
    

    I hope this helps someone else someday!

    0 讨论(0)
  • 2020-11-27 04:02

    Unless the array is sorted, that's the best you're going to get. If it is sorted, just take the first and last elements.

    Of course, if it's not sorted, then sorting first and grabbing the first and last is guaranteed to be less efficient than just looping through once. Even the best sorting algorithms have to look at each element more than once (an average of O(log N) times for each element. That's O(N*Log N) total. A simple scan once through is only O(N).

    If you are wanting quick access to the largest element in a data structure, take a look at heaps for an efficient way to keep objects in some sort of order.

    0 讨论(0)
提交回复
热议问题