Algorithm 时间复杂性分析:更好的解释?

Algorithm 时间复杂性分析:更好的解释?,algorithm,time-complexity,Algorithm,Time Complexity,我偶然发现了下面的代码来查找满足三角形和属性的三元组 // Function to count all possible triangles with arr[] // elements static int findNumberOfTriangles(int arr[]) { int n = arr.length; // Sort the array elements in non-decreasing order Arr

我偶然发现了下面的代码来查找满足三角形和属性的三元组

// Function to count all possible triangles with arr[]
    // elements
    static int findNumberOfTriangles(int arr[])
    {
        int n = arr.length;
        // Sort the array elements in non-decreasing order
        Arrays.sort(arr);

        // Initialize count of triangles
        int count = 0;

        // Fix the first element.  We need to run till n-3 as
        // the other two elements are selected from arr[i+1...n-1]
        for (int i = 0; i < n-2; ++i)
        {
            // Initialize index of the rightmost third element
            int k = i + 2;

            // Fix the second element
            for (int j = i+1; j < n; ++j)
            {
                /* Find the rightmost element which is smaller
                   than the sum of two fixed elements
                   The important thing to note here is, we use
                   the previous value of k. If value of arr[i] +
                   arr[j-1] was greater than arr[k], then arr[i] +
                   arr[j] must be greater than k, because the
                   array is sorted. */
                while (k < n && arr[i] + arr[j] > arr[k])
                    ++k;

               /* Total number of possible triangles that can be
                  formed with the two fixed elements is k - j - 1.
                  The two fixed elements are arr[i] and arr[j].  All
                  elements between arr[j+1] to arr[k-1] can form a
                  triangle with arr[i] and arr[j]. One is subtracted
                  from k because k is incremented one extra in above
                  while loop. k will always be greater than j. If j
                  becomes equal to k, then above loop will increment
                  k, because arr[k] + arr[i] is always/ greater than
                  arr[k] */
                count += k - j - 1;
            }
        }
        return count;
    }
//使用arr[]计算所有可能三角形的函数
//元素
静态int findNumberOfTriangles(int arr[])
{
int n=阵列长度;
//按非降序排列数组元素
数组。排序(arr);
//初始化三角形的计数
整数计数=0;
//修复第一个元素。我们需要运行到n-3
//其他两个元素从arr[i+1…n-1]中选择
对于(int i=0;iarr[k])
++k;
/*可以删除的可能三角形的总数
由两个固定元素组成的是k-j-1。
这两个固定元素是arr[i]和arr[j]
arr[j+1]到arr[k-1]之间的元素可以形成
带arr[i]和arr[j]的三角形。减去一
从k开始,因为k在上面额外增加了一个
而loop.k总是大于j
变为等于k,则上面的循环将递增
k、 因为arr[k]+arr[i]总是/大于
arr[k]*/
计数+=k-j-1;
}
}
返回计数;
}

有人能更好地解释为什么这个解决方案的时间复杂度是O(n^2)而不是O(n^3)?我的理解是,对于每个i和j,k也是不同的。

唯一可以执行超过O(n^2)次的语句是嵌套最多的
++k
语句


但是
k
从不超过
n
,并且被重置(为非负数)
n-2次。这证明了
++k
语句最多执行
n(n-2)=O(n^2)
次。

上述解决方案的时间复杂度是
O(n^2)
,因为您可以看到
k
的值在第二个
for
循环之前被初始化。在第二个
中
循环
k
的值在
条件下增加,而
条件下增加。一旦
while
条件终止,for
循环将运行
j
的下一个值,并且
k
的值与之前在
循环中终止时保持相同。
一旦
k
的值等于
n
之后,它将不会运行
j
的任何值

因此,第二个for循环仅从
k=i+2
运行到
n
。因此复杂性是
O(n^2)