Big O Efficiency Lab: Linear O(n), Quadratic O(n^2), and Triangular O(n^2)
Compare with Nexo the exact operation counts of three processing engines using single, quadratic, and triangular loops against their asymptotic Big O classification.
At the Código con Criterio efficiency lab, Nexo is comparing three processing engines to demonstrate the difference between exact operation counts and asymptotic Big O growth order.
Implement the required functions algoritmoLineal(n), algoritmoCuadratico(n), algoritmoTriangular(n), and solve(input), which receives:
tamano: integer between 0 and 100.
repeticiones: integer between 1 and 20.
Requirements for the Three Functions
algoritmoLineal(n):
Initialize a counter at 0.
Loop i from 0 to n - 1 with a for loop, incrementing the counter by 1 per iteration, and return the total (n operations).
algoritmoCuadratico(n):
Initialize a counter at 0.
Use two nested for loops: outer i from 0 to n - 1 and inner j from 0 to n - 1, incrementing the counter by 1 on each inner iteration (n * n operations).
algoritmoTriangular(n):
Initialize a counter at 0.
Use two nested for loops: outer i from 0 to n - 1 and inner j from 0 to i inclusive, incrementing the counter by 1 on each inner iteration (n * (n + 1) / 2 operations).
Coordination in solve(input)
Use a for loop to call each of the three functions with tamano exactly repeticiones times and accumulate their operations into three counters.
Compute totalOperaciones as the sum of the three accumulated totals.
Determine algoritmoMenorTrabajo by comparing the three accumulated totals: return "LINEAL", "CUADRATICO", or "TRIANGULAR" if there is a single strict minimum, or "EMPATE" if two or three algorithms share the minimum value.