Jim Mahoney | March 1 2018
import numpy as np
import matplotlib.pyplot as plt
import csv, os
% matplotlib inline
os.sys.version
First step: read in the data, and store it in a way that we have (x,y) lists of points to be plotted.
I'm leaving out the times which were 0.0 : those can't be right, and they can't be put on a log-log plot.
data = {} # data[algorithm] = {'n': [], 'times[]}
with open('results.csv') as csvfile:
dictreader = csv.DictReader(csvfile)
data = {}
for line in dictreader:
algorithm = line['algorithm']
if not algorithm in data:
data[algorithm] = {'size':[], 'time':[]}
n = int(line['size'])
t = float(line['time'])
if t > 0:
data[algorithm]['size'].append(n)
data[algorithm]['time'].append(t)
Here's what we have.
import pprint
pp = pprint.PrettyPrinter()
pp.pprint(data)
And now we plot it.
To see the O() behavior, a log-log plot is the most appropriate.
plt.figure(dpi=200, figsize=(8,6)) # plot size in inches
plt.title('sorting times')
plt.xlabel('size')
plt.ylabel('time [sec]')
plt.ylim(1e-9, 10)
plt.xlim(8, 50000)
algos = ['bubble', 'insertion', 'selection']
colors = ['r', 'g', 'b'] # red, green, blue
for i in range(3):
algo = algos[i]
x = np.array(data[algo]['size'])
y = np.array(data[algo]['time'])
plt.loglog(x, y, colors[i])
plt.loglog(x, y, colors[i]+'o')
plt.loglog(x, 1e-10 * x*x, 'y')
plt.legend([algos[0], algos[0], algos[1], algos[1], algos[2], algos[2], 'O(n2)'],
loc=4) # location lower right
plt.show()
We see that all three of these algorithms run in $O(n^2)$ time.