[prev] 5 [next]

Program/Algorithm Efficiency (cont)

Example: finding max value in an unsorted array

int findMax(int a[], int N) {
   int i, max = a[0];
   for (i = 1; i < N; i++)
      if (a[i] > max) max = a[i];
   return max;
}

Core operation? ... compare a[i] to max

How many times? ... clearly N-1

Execution cost grows linearly   (i.e. 2 × #elements ⇒ 2 × time)