In [1]:
from algzoo.sorting.insertion import insertion_sort
from algzoo.sorting.merge import merge_sort
from random import random
import time
n_range = [round(10 ** (i/10)) for i in range(40)]
merge_duration = []
insert_duration = []
In [2]:
def profile_random_sort(function, n):
unsorted = [random() for _ in range(n)]
start = time.time()
function(unsorted)
end = time.time()
return end-start
In [3]:
for n in n_range:
merge_duration.append(profile_random_sort(merge_sort, n))
insert_duration.append(profile_random_sort(insertion_sort, n))
In [6]:
import matplotlib.pyplot as plt
h1 = plt.semilogx(n_range, insert_duration, label='Insertion')
h2 = plt.semilogx(n_range, merge_duration, label='Merge')
plt.grid(True)
plt.xlabel('n')
plt.ylabel('Random sort duration / s')
plt.show()
In [ ]: