简体   繁体   English

查找数组中最小值和最大值的有效方法

[英]Efficient way for finding the min and max value in an array

I want to find out the minimum and maximum value in an array of integers. 我想找出整数数组中的最小值和最大值。

Which one of the following ways would be the more efficient? 以下哪种方式效率更高?

  1. Sort the array and then look at start and end to get the minimum and maximum. 对数组进行排序,然后查看开始和结束以获得最小值和最大值。

  2. Convert the array into a list using Arrays.asList() and then use the Collections.min() method. 使用Arrays.asList()将数组转换为列表,然后使用Collections.min()方法。

The code where I want to use this is the following: 我想要使​​用它的代码如下:

// Find missing number from an array of consecutive numbers arranged randomly
import java.util.Arrays;

public class MissingNumber {

    public static void main(String[] args) {

        int[] consecutiveRandomNos = { 3, 6, 5 };

        System.out.println(addNumbers(consecutiveRandomNos));
        System.out.println("The missing number is "
                        + (returnSum(consecutiveRandomNos) - addNumbers(consecutiveRandomNos)));
    }

    public static int addNumbers(int... numbers) {
        int result = 0;

        for (int number : numbers) {
            result += number;
        }

        return result;
    }

    public static int returnSum(int... nos) {

        Arrays.sort(nos);

        int max = nos[nos.length - 1];

        int min = nos[0];

        int total = 0;

        for (int i = min; i <= max; i++) {
            total += i;
        }

        return total;
    }
}

Sort is O(Nlog(N)) at best. 排序最多为O(Nlog(N))。 You can find min and max trivially in O(n) just iterating over the array. 您可以在O(n)中轻松地找到min和max,只是遍历数组。

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i=0; i<array.length; i++)
{
    if(array[i] < min)
       min = array[i]
    if(array[i] > max)
       max = array[i]
}

Edit: 编辑:


I noticed you pasted some extra code and that you actually want to find a missing number in an array of consecutive numbers. 我注意到你粘贴了一些额外的代码,并且你实际上想要在一个连续数字的数组中找到一个缺失的数字。 Rather than iterating all that much, there are mathematical summations that can help you here in O(1). 而不是迭代那么多,有数学总结可以帮助你在这里O(1)。 In fact, you can solve the entire problem with a single for loop: 实际上,您可以使用单个for循环解决整个问题:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
for(int i=0; i<array.length; i++)
{
    if(array[i] < min)
       min = array[i];
    if(array[i] > max)
       max = array[i];
    sum += array[i];
}

return (max - min + 1)(max + min)/2 - sum;

sorting costs O(NlogN), going through an array to find min and max costs O(N). 分类成本O(NlogN),通过一个数组来查找最小和最大成本O(N)。 No need for convertsion to list, just iterrate the array. 无需转换列表,只需对数组进行处理即可。

Collection#min source code: Collection#min源代码:

585     public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) {
586         Iterator<? extends T> i = coll.iterator();
587         T candidate = i.next();
588 
589         while (i.hasNext()) {
590             T next = i.next();
591             if (next.compareTo(candidate) < 0)
592                 candidate = next;
593         }
594         return candidate;
595     }

It's O(n) in terms of time complexity. 就时间复杂度而言,它是O(n)。 If your sorting algorithm is O(n) (post it please), they're both the same, again, in terms of time complexity. 如果您的排序算法是O(n)(请发布),它们在时间复杂度方面也是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM