Big o 最坏情况下的大O运行时

Big o 最坏情况下的大O运行时,big-o,time-complexity,Big O,Time Complexity,有人能帮我找到以下算法在n方面的最坏情况big-O运行时吗 // precondition: A contains only positive numbers public int[] four(int A[]) { int n=A.length; int[] B = new int[n]; int max; for (int k=n-1; k >= 0; k--) { max = findMax(A); //call to findMax

有人能帮我找到以下算法在
n
方面的最坏情况big-O运行时吗

// precondition: A contains only positive numbers
public int[] four(int A[])
{
    int n=A.length;
    int[] B = new int[n];
    int max;
    for (int k=n-1; k >= 0; k--) {
        max = findMax(A);  //call to findMax above
        B[k]=A[max];
        A[max]=-1;
    }
    return B; 
}
整个操作将花费O(n^2)时间,因为循环运行了O(n)次,其中一个操作的时间复杂度为O(n),假设findmax()将花费O(n),这在[]是加扰数组的情况下很常见


这本身看起来是使用2个数组进行选择排序。

代码的复杂性取决于
findMax()的复杂性。

当算法从
n-1
0
计数一次时,时间复杂度为
O(n⋅f(n))
,其中
f(n)
findMax()的复杂性

A
被假定为未排序的,因为您正在对其进行排序。所以
findMax()
可能是一个复杂度为
O(n)
的线性搜索


因此,总体复杂性将是
O(n²)

取决于findMax,否则它只是O(n),其中n是A的长度。如何实现
findMax()
// precondition: A contains only positive numbers
public int[] four(int A[])
{
    int n=A.length; //constant operation
    int[] B = new int[n]; //constant operation
    int max; //constant operation
    for (int k=n-1; k >= 0; k--) { //iterates through n times
        max = findMax(A);  //call to findMax above //will take complexity of O(n) assuming A is scrambled, 
        B[k]=A[max]; //constant operation
        A[max]=-1; //constant operation
    }
    return B; //constant operation 
}