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

后端 未结 18 831
日久生厌
日久生厌 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:37

    If you are building the array once and want to find the maximum just once, iterating is the best you can do.

    When you want to modify the array and occasionally want to know the maximum element, you should use a Priority Queue. One of the best data structures for that is a Fibonacci Heap, if this is too complicated use a Binary Heap which is slower but still good.

    To find minimum and maximum, just build two heaps and change the sign of the numbers in one of them.

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

    Algorithm MaxMin(first, last, max, min)

    //This algorithm stores the highest and lowest element

    //Values of the global array A in the global variables max and min

    //tmax and tmin are temporary global variables

    {
    if (first==last) //Sub-array contains single element
     {
        max=A[first];
        min=A[first];
     }
     else if(first+1==last) //Sub-array contains two elements
      {
         if(A[first]<A[Last])
          {
          max=a[last];  //Second element is largest
          min=a[first]; //First element is smallest
          }
       else
       {
         max=a[first]; //First element is Largest 
         min=a[last];  //Second element is smallest
       }
    }
    else
     //sub-array contains more than two elements
    {
     //Hence partition the sub-array into smaller sub-array 
     mid=(first+last)/2;
     //Recursively solve the sub-array
     MaxMin(first,mid,max,min);
     MaxMin(mid+1,last,tmax,tmin);
     if(max<tmax)
      {
         max=tmax;
      }
        if(min>tmin)
      {
       min=tmin;
      }
     }
    }
    
    0 讨论(0)
  • 2020-11-27 03:40

    There isn't any reliable way to get the minimum/maximum without testing every value. You don't want to try a sort or anything like that, walking through the array is O(n), which is better than any sort algorithm can do in the general case.

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

    If you want to find both the min and max at the same time, the loop can be modified as follows:

    int min = int.maxValue;
    int max = int.minValue;
    
    foreach num in someArray {
      if(num < min)
        min = num;
      if(num > max)
        max = num;
    }
    

    This should get achieve O(n) timing.

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

    Find max values from a array Let's see how to obtain min, max values by using a single funtion

    public void findMaxValue(){
       int[] my_array = {1,2,,6,5,8,3,9,0,23};
       int max = my_array[0];
       for(int i=1; i<my_array.length; i++)
       {
          if(my_array[i] > max)
             max = my_array[i];
       }
       return max; 
    }
    

    same thing can do for find min value

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

    Depends on what you call "best." From a theoretical point of view, you cannot solve the problem in less than O(n) in a deterministic Turing machine.

    The naive algorithm is too loop and update min, max. However, a recursive solution will require less comparisons than naive algorithm, if you want to get min, max simultaneously (it isn't necessarily faster due to function call overhead).

    struct MinMax{
       public int Min,Max;
    }
    
    MinMax FindMinMax(int[] array, int start, int end) {
       if (start == end)
          return new MinMax { Min = array[start], Max = array[start] };
    
       if (start == end - 1)
          return new MinMax { Min = Math.Min(array[start], array[end]), Max = Math.Max(array[start], array[end]) } ;
    
       MinMax res1 = FindMinMax(array, start, (start + end)/2);
       MinMax res2 = FindMinMax(array, (start+end)/2+1, end);
       return new MinMax { Min = Math.Min(res1.Min, res2.Min), Max = Math.Max(res1.Max, res2.Max) } ;
    }
    

    The simplest solution would be to sort and get the first and last item, though it's obviously not the fastest ;)

    The best solution, performance-wise, to find the minimum or maximum is the naive algorithm you written (with a single loop).

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