The Question: Which Complexity has my function? and How to find time complexity of my algorithm?
The function checks if a given int array is sorted.
my Code:
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;
}
<constant? Is it linear in the number of digits?d.lengthin this case) and the machine model seems somewhat irrelevant if you assume everything executes sequentially (unless specified otherwise). "The time complexity of algorithm X is O(Y)" is something I've seen countless times from countless sources without any of the details you mentioned.