尝试进行大 O 分析-该程序的平均情况是什么?O(n/2(n/2)) = O(n^2) ?..
/* Returns the largest integer in the array */
int CompareToAll(int array[], int n)
{
int i, j;
bool isMax;/* Make sure that there is at least one element in the array.*/
if (n <= 0) return -1;
for (i = n-1; i > 0; i--)
{
isMax = true;
for (j = 0; j < n; j++) {
/* See if any value is greater.*/
if (array[j] > array[i]){
isMax = false; /* array[i] is not the largest value. */
break;
}
} /* If isMax is true, no larger valueexists; array[i] is max. */
if (isMax)
break;
}
return array[i];
}
谢谢