我的算法的复杂性

War*_* S. 0 c++ algorithm complexity-theory time-complexity

问题:哪个复杂性有我的功能?以及如何找到算法的时间复杂度?

该函数检查给定的int数组是否已排序.

我的代码:

public static boolean isSorted(double d[]){
boolean sortedAscending = true;
boolean sortedDescending = true;

boolean bool = false;
for (int i = 0; i < d.length-1; i++) {
    if(d[i] > d[i+1] && sortedAscending){
        sortedAscending = false;
        if(bool){
            break;
        }
        bool = true;
    }
    else if(d[i] < d[i+1]&& sortedDescending){
        sortedDescending = false;
        if(bool){
            break;
        }
        bool = true;
    }
}
return sortedAscending || sortedDescending;
}
Run Code Online (Sandbox Code Playgroud)

Kai*_*dul 5

这只是一个循环程序,在每次迭代中都有恒定的时间执行.时间复杂度是线性的 - 数组长度O(n)在哪里n.