1 2 3 4 5 6 |
|
The above code runs in .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The algorithm is finding the maximum value in the array by starting with a value of zero for the variable 'max'. It scans through each element of the array in turn, comparing it with the current value of the variable 'max', changing the value if at any point a new maximum is found. Each number in the array is compared with 'max' once. Therefore assuming that each comparison takes equal time, the upper bound on the computing time is O(n), since there is an intuitive proportionality between number of elements and computing time.