I want to know if my concept and understanding of the code is correct or not! Here, at first, I set the last number as the max, and then I am using another for loop to compare each value to all other values to find the largest value right? Also is the runtime of this is O(n^2) since here two for loop is used? I know there exists a better linear solution(O(n)) but I want to manually see and check how much time it will take to execute it and also trying to compare the efficiency between two. I also do not know what is the space complexity of this code. Any further explanation will be greatly appreciated.
/*The following code will return the largest value in an array of non-negative integers */
int CompareToAll (int array[], int n)
{
int i, j;
bool isMax;
if (n <= 0)
return -1;
for (i = n-1; i > 0; i--) {
isMax = true;
for (j = 0; j < n; j++) {
if (array[j] > array[i]) {
isMax = false;
break;
}
}
if (isMax) break;
}
return array[i];
}