I calculate the optimal case complexity, average, and worst of this algorithm in java, I think if good is O (1) in the worst case is O (n), but I do not know if average! could you help me on how to calculate it? thank you!
public boolean searchFalse(boolean[] b){
boolean trovato=false;
for(int i=0;i<b.length;i++){
if(b[i]==false){
trovato=true;
break;
}
}return trovato;
}
I couldn’t resist re-writing it
This stops after the first element potentially O(1).
If all the boolean are random the average search time is O(1) as you perform 2 searches on average, or if there is typically one false value in a random position the average is O(N)
If it has to search all the way, the worst case is O(N)
In short O(N/2) = O(N)