Big Big O

1
2
3
4
5
6
def find_max(Arr):
    max = 0
    for num in Arr:
        if num > max:
            max = num
    return max

The above code runs in __________ \text{\_\_\_\_\_\_\_\_\_\_} .


This problem is a part of the set: Efficient Algorithms .
O ( log n ) O(\log n) O ( n ) O(n) O ( 1 ) O(1) O ( n log n ) O(n\log n)

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.

1 solution

David Orrell
Jan 31, 2017

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.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...