简体   繁体   English

O(n ^ 2)复杂度

[英]O(n^2) complexity

Which one of the following has the O(n^2 ) complexity 以下哪一项具有O(n^2 )复杂度

public boolean findDuplicates(int[] inputData) {
        for (int i = 0; i < inputData.length; i++) {
            for (int j = 0; j < inputData.length; j++) {
                if (inputData[i] == inputData[j] && i != j) {
                    return true;
                }
            }
        }
        return false;
    }

vs

public boolean findDuplicates(int[] inputData) {
        for (int i = 0; i < inputData.length; i++) {
            for (int j = 0; j < inputData.length; j++) {
             System.out.println("...");
            }
        }
        return false;
    }

does if (inputData[i] == inputData[j] && i != j) { return true; } if (inputData[i] == inputData[j] && i != j) { return true; } if (inputData[i] == inputData[j] && i != j) { return true; } in the first loop break the complexity of O(n^2) as I see I will match only 2 elements if length of the inputDate array is 2 . if (inputData[i] == inputData[j] && i != j) { return true; }在第一回路打破的复杂性O(n^2)因为我看到,如果inputDate阵列的长度是2 I仅匹配2的元件。

I'm sorry if this noob question, but what I don't understand is that complexity refers to the total elemens iterated or total of condition satisfied ? 如果这个菜鸟问题很抱歉,但是我不明白,复杂性是指迭代的元素总数或满足的条件总数吗?

and how about this one (assuming We don't have to calculate the exact complexity, and assuming we ignore index out of bounds in the inner loop), is this 以及这个(假设我们不必计算确切的复杂度,并假设我们忽略内循环中的索引超出范围 )如何?

public boolean findDuplicates(int[] inputData) {
        for (int i = 0; i < inputData.length; i++) {
            for (int j = 1; j < inputData.length; j++) {
            ....
            }
        }
        return false;
    }

still O(n^2) ? 还是O(n^2)吗?

Both of the methods you posted have O(n^2) complexity. 您发布的两种方法都具有O(n ^ 2)的复杂度。 The conditional inside the first one doesn't change the big O. 第一个内部的条件不会改变大O。

big O notation describes the limiting behavior of a function when the argument tends towards a particular value or infinity. 大O符号表示当参数趋于特定值或无穷大时函数的限制行为。

I think it is fairly clear to you that the second scenario will have O(n^2) time complexity. 我认为您很清楚第二种情况的时间复杂度为O(n ^ 2)。

In the first case, unless you can always ensure that you will find a duplicate in the first k iterations, where k is a constant which does not depend on n, it will have a complexity of O(n) [Since O(kn) where k is a constant, however large, but known, is O(n)] 在第一种情况下,除非总能确保在前k个迭代中找到重复项,其中k是不依赖于n的常数,否则它将具有O(n)的复杂度[因为O(kn)其中k是一个常数,但是很大,但是已知,是O(n)]

However, if this k depends on n in any manner (say, you will always find a match in the first half of the array which repeats), or cannot guarantee a match for every run, then the complexity will be O(n^2). 但是,如果此k以任何方式取决于n(例如,您总是会在重复的数组的前半部分找到匹配项),或者不能保证每次运行都匹配,那么复杂度将为O(n ^ 2 )。 [O(n*n/k) = O(n^2) where k is a constant. [O(n * n / k)= O(n ^ 2),其中k为常数。 Here, k is an arbitrary constant which helps find the percentage of array you have to go through before finding the first index of repeating element] 在这里,k是一个任意常数,可以帮助找到在找到重复元素的第一个索引之前必须经过的数组的百分比]

EDIT: 编辑:

Did not notice your edit earlier. 之前没有注意到您的编辑。 Yes, the third case is also O(n^2) You can also do the following optimization: 是的,第三种情况也是O(n ^ 2)您还可以进行以下优化:

findDuplicates(int[] arr) {
    for (int i = 0; i < arr.size(); i++) {
        for (int j = i+1; j < arr.size(); j++) {
            ....
        }
    }
}

The above case also has a complexity of O(n^2), as it will go through the loop for n + n-1 + n-2 + .... + 1 times, which is (n*n+1/2), which is O(n^2) 上面的情况还具有O(n ^ 2)的复杂度,因为它将经过循环n + n-1 + n-2 + .... + 1次,即(n * n + 1 / 2),即O(n ^ 2)

All of above loops are O(n^2). 以上所有循环均为O(n ^ 2)。
algorithms are analyzed normally in best, average and worst case scenarios. 通常在最佳,平均和最坏情况下分析算法。

For your loops with condition: Worst case: O(n^2) 对于有条件的循环:最坏的情况:O(n ^ 2)
Best case: Constant time. 最好的情况:恒定时间。 Because best scenario would be inputData[0]==inputData 1 因为最好的情况是inputData [0] == inputData 1

For your loops without condition: 对于无条件的循环:
Now, it becomes just nested array traversal so, both worst and best case will be O(n^2). 现在,它变成嵌套数组遍历,因此,最坏情况和最佳情况均为O(n ^ 2)。

Overall, worst case performance is used for evaluating algorithms but few algorithms works (eg Quicksort ) really well in average case compared to worst case which. 总体而言,最坏情况下的性能用于评估算法,但与最坏情况相比,在一般情况下很少有算法(例如Quicksort )能很好地工作。

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

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