Finding smallest value in an array most efficiently

后端 未结 13 1637
慢半拍i
慢半拍i 2020-11-29 07:02

There are N values in the array, and one of them is the smallest value. How can I find the smallest value most efficiently?

相关标签:
13条回答
  • 2020-11-29 07:38

    Richie's answer is close. It depends upon the language. Here is a good solution for java:

    int smallest =  Integer.MAX_VALUE;
    int array[]; // Assume it is filled.
    int array_length = array.length;
    for (int i = array_length - 1; i >= 0; i--) {
        if (array[i] < smallest) {
            smallest = array[i];
        }
    }
    

    I go through the array in reverse order, because comparing "i" to "array_length" in the loop comparison requires a fetch and a comparison (two operations), whereas comparing "i" to "0" is a single JVM bytecode operation. If the work being done in the loop is negligible, then the loop comparison consumes a sizable fraction of the time.

    Of course, others pointed out that encapsulating the array and controlling inserts will help. If getting the minimum was ALL you needed, keeping the list in sorted order is not necessary. Just keep an instance variable that holds the smallest inserted so far, and compare it to each value as it is added to the array. (Of course, this fails if you remove elements. In that case, if you remove the current lowest value, you need to do a scan of the entire array to find the new lowest value.)

    0 讨论(0)
  • 2020-11-29 07:39

    The stl contains a bunch of methods that should be used dependent to the problem.

    std::find
    std::find_if
    std::count
    std::find
    std::binary_search
    std::equal_range
    std::lower_bound
    std::upper_bound
    

    Now it contains on your data what algorithm to use. This Artikel contains a perfect table to help choosing the right algorithm.


    In the special case where min max should be determined and you are using std::vector or ???* array

    std::min_element
    std::max_element
    

    can be used.

    0 讨论(0)
  • 2020-11-29 07:39
    //find the min in an array list of #s
    $array = array(45,545,134,6735,545,23,434);
    
    $smallest = $array[0];
    for($i=1; $i<count($array); $i++){
        if($array[$i] < $smallest){
            echo $array[$i];
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:42

    If you're developing some kind of your own array abstraction, you can get O(1) if you store smallest added value in additional attribute and compare it every time a new item is put into array.

    It should look something like this:

    class MyArray
    {
    public:
        MyArray() : m_minValue(INT_MAX) {}
    
        void add(int newValue)
        {
            if (newValue < m_minValue) m_minValue = newValue;
            list.push_back( newValue );
        }
    
        int min()
        {
            return m_minValue;
        }
    
    private:
        int m_minValue;
        std::list m_list;
    }
    
    0 讨论(0)
  • 2020-11-29 07:46

    If you want to be really efficient and you have enough time to spent, use SIMD instruction.

    You can compare several pairs in one instruction:

    r0 := min(a0, b0)
    r1 := min(a1, b1)
    r2 := min(a2, b2)
    r3 := min(a3, b3)
    __m64 _mm_min_pu8(__m64 a , __m64 b );
    

    Today every computer supports it. Other already have written min function for you:

    http://smartdata.usbid.com/datasheets/usbid/2001/2001-q1/i_minmax.pdf

    or use already ready library.

    0 讨论(0)
  • 2020-11-29 07:48

    If they are unsorted, you can't do much but look at each one, which is O(N), and when you're done you'll know the minimum.


    Pseudo-code:

    small = <biggest value> // such as std::numerical_limits<int>::max
    for each element in array:
        if (element < small)
            small = element
    

    A better way reminded by Ben to me was to just initialize small with the first element:

    small = element[0]
    for each element in array, starting from 1 (not 0):
        if (element < small)
            small = element
    

    The above is wrapped in the algorithm header as std::min_element.


    If you can keep your array sorted as items are added, then finding it will be O(1), since you can keep the smallest at front.

    That's as good as it gets with arrays.

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