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)