-3

i'm making an algorithm and I have to do the Big O, but I'dont how to do it, can any one help me? The code is this. I added the time library to be able to see the execution time in the algorithm, but I don't know very well how to calculate Big O

import time
arre=[1, 3, 2, 4, 5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
m=2
sumatoria=sum(arre)
cantidad=sumatoria/m
arre.sort(reverse=True)
arreglo=[]
arrAuxiliar=arre.copy()
definitivo = []

if sumatoria % m==0:
    for idx in range(m):
        if arre[idx] == cantidad:
            aux=[arre[idx]]
            definitivo.append(aux)
            arrAuxiliar.remove(arre[idx])
        else:
            arreglo.clear()
            arreglo.append(arre[idx])
            arrAuxiliar.remove(arre[idx])
            if cantidad-arre[idx] in arrAuxiliar:
                numero=arre[arre.index(cantidad-arre[idx])]
                arreglo.append(numero)
                arrAuxiliar.remove(numero)
            else:
                if idx==m-1:
                    arreglo+=arrAuxiliar
            definitivo.append(arreglo.copy())

print(definitivo)
print("El tiempo de ejecucion es: ", time.time())
Daniel loaiza
  • 105
  • 1
  • 2
  • 8
  • 1
    Does this answer your question? [What is a plain English explanation of "Big O" notation?](https://stackoverflow.com/questions/487258/what-is-a-plain-english-explanation-of-big-o-notation) – DelfikPro Jun 11 '21 at 02:40

2 Answers2

1

Big-O is a notation that is used to calculate the complexity of an algorithm and it cannot be calculated that way, the complexity of an algorithm like O(1) is when you apply a formula to solve a problem, when you use O(n) is when you use a loop, O(n^2) is when you use nested loops, but it cannot be calculated that way. You have to avoid to use algorithms with notation O(n^2) because with each algorithm perform more operations and consume more time.

https://www.geeksforgeeks.org/analysis-algorithms-big-o-analysis/

Br0k3nS0u1
  • 51
  • 5
0

Your current run time is O(N) if the first of triggers. The N is equal to m in your code. All this is also assuming the following actions all take constant time (which I think it does).

Generally, you can think of Big O as how many times you must go through something or iterate. If you have a for loop that runs N time, then you take O(N) time. Now in the for loop, if you are doing some action that takes time to calculate, you could have all kinds of weird timing. The first comment provided a very detailed explanation and you should take the time to read it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Joey Zhao
  • 59
  • 6