Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pranavgarg/58744db65aed50f0dbe7f99217228ffa to your computer and use it in GitHub Desktop.
Save pranavgarg/58744db65aed50f0dbe7f99217228ffa to your computer and use it in GitHub Desktop.
plot runtimes for different big oh notation
import matplotlib.pyplot as plt
import numpy as np
import math
n = range(1,9) # input range
lgn = [math.log(x,2) for x in n] #logn
l = [x for x in n] # linear
nlgn = [x*math.log(x,2) for x in n] # nlogn
q = [x**2 for x in n] # quadratic
p = [2**i for i in n] # polynomial
plt.plot(n, lgn, label="logn")
plt.plot(n, l, label="linear")
plt.plot(n,nlgn, label="nlogn")
plt.plot(n,q, label="quadratic")
plt.plot(n,p, label='polynomial')
plt.legend(loc='upper left')
plt.xlabel('input range values (n)')
plt.ylabel('time range on different runtimes')
plt.title('runtime complexity')
plt.grid(True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment