-2

Need help with these two examples. As I kind of understand Big Oh, but not really the c and No concepts. First one seems pretty straight forward. I'm pretty sure the Big Oh would be O(n^3) but I'm not sure.

f(x) = 2n3 + 5n + 2

The next one is the one that really makes me feel like idk what I'm doing.

def analyze(alist):
        1   exchanges = True
        2   passnum = len(alist)-1
        3   while passnum > 0 and exchanges:
        4       exchanges = False
        5       for i in range(passnum):
        6              if alist[i]>alist[i+1]:
        7               exchanges = True
        8                  alist[i],alist[i+1]=alist[i+1],alist[i]
        9          passnum = passnum-1

HE wants me to label each line regarding Big Oh (what?), and then calculate the Big Oh, c, and No. Any help/explanation would be a huge help, I'm feeling lost. THought I had it, but its clear that I do not. Thanks

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
A.Phil
  • 1
  • 1

1 Answers1

0

Each line represents either an elementary operation or a function.

An elementary operation is often considered to take O(1) time (constant time).

if i had this simple code and i'm looking the big Oh relative to N, a number given in argument

a = 1 // O(1) assignement is constant 
b = a + 2 // O(1) addition is constant
for (a=0 to N-1) // a will take value  between [0..n-1], which is n different values
     a = a + 1  // addition is O(1)
//resume: i have => O(1) + O(1) + N * O(1) => O(N)

Here you see i did label each line, and i can conclude , this code is O(N) cause it's the dominant factor. You need to try to do the same for your snippet of code.

Julien Rousé
  • 1,115
  • 1
  • 15
  • 30