This is a Part2 to this question where the task was to plot the data that I got from my time measurements.
I've never worked with Jupyter Notebooks (ipynb
) since most people I talked to were critiquing (in a bad way) them.
A lot of repetition is noticeable, that's because I haven't gotten the hang of how the code blocks work (I thought they work separately -> lifetime of variables).
Edit: .ipynb
files are a nightmare to format in here, I used:
###
# Plot [n] placeholder (indicated where the plot is)
###
###
# new cell (indicates new cell)
###
Code:
lists = [[]]
sizes = [10, 100, 1000, 10000, 100000]
current_list = 0
with open('data.txt', 'r') as file:
lines = file.readlines()
for line in lines:
line = line.strip()
if line == "":
lists.append([])
current_list += 1
elif line.startswith('[time]'):
time = int(line.split(':')[1].strip())
lists[current_list].append(time)
ms1_times = lists[0]
qs1_times = lists[1]
hs1_times = lists[2]
ms2_times = lists[3]
qs2_times = lists[4]
hs2_times = lists[5]
ms3_times = lists[6]
qs3_times = lists[7]
hs3_times = lists[8]
###
# new cell
###
import matplotlib.pyplot as plt
sizes = [10, 100, 1000, 10000, 100000]
ms1_times = lists[0]
ms2_times = lists[3]
ms3_times = lists[6]
plt.figure(figsize=(10, 6))
plt.plot(sizes, ms1_times, marker='o', label='MS #1')
plt.plot(sizes, ms2_times, marker='s', label='MS #2')
plt.plot(sizes, ms3_times, marker='^', label='MS #3')
plt.xlabel('Sizes')
plt.ylabel('Times [ms]')
plt.title('Comparison of Measure Sort Execution')
plt.legend()
plt.show()
###
# Plot 1 placeholder
###
###
# new cell
###
import matplotlib.pyplot as plt
sizes = [10, 100, 1000, 10000, 100000]
ms1_times = lists[1]
ms2_times = lists[4]
ms3_times = lists[7]
plt.figure(figsize=(10, 6))
plt.plot(sizes, ms1_times, marker='o', label='QS #1')
plt.plot(sizes, ms2_times, marker='s', label='QS #2')
plt.plot(sizes, ms3_times, marker='^', label='QS #3')
plt.xlabel('Sizes')
plt.ylabel('Times [ms]')
plt.title('Comparison of Quick Sort Execution')
plt.legend()
plt.show()
###
# Plot 2 placeholder
###
###
# new cell
###
import matplotlib.pyplot as plt
sizes = [10, 100, 1000, 10000, 100000]
ms1_times = lists[2]
ms2_times = lists[5]
ms3_times = lists[8]
plt.figure(figsize=(10, 6))
plt.plot(sizes, ms1_times, marker='o', label='HS #1')
plt.plot(sizes, ms2_times, marker='s', label='HS #2')
plt.plot(sizes, ms3_times, marker='^', label='HS #3')
plt.xlabel('Sizes')
plt.ylabel('Time [ms]')
plt.title('Comparison of Heap Sort Execution')
plt.legend()
plt.show()
###
# Plot 3 placeholder
###
###
# new cell
###
import matplotlib.pyplot as plt
ms_10 = ms1_times[0]
qs_10 = qs1_times[0]
hs_10 = hs1_times[0]
ms_100 = ms1_times[1]
qs_100 = qs1_times[1]
hs_100 = hs1_times[1]
ms_1000 = ms1_times[2]
qs_1000 = qs1_times[2]
hs_1000 = hs1_times[2]
x = ['10', '100', '1000']
y_ms = [ms_10, ms_100, ms_1000]
y_qs = [qs_10, qs_100, qs_1000]
y_hs = [hs_10, hs_100, hs_1000]
plt.figure(figsize=(10, 6)) # Width: 8 inches, Height: 6 inches
plt.scatter(x, y_hs, c='red', label='Heap Sort')
plt.plot(x, y_hs, c='red', linestyle='-')
plt.scatter(x, y_ms, c='blue', label='Merge Sort')
plt.plot(x, y_ms, c='blue', linestyle='--')
plt.scatter(x, y_qs, c='green', label='Quick Sort')
plt.plot(x, y_qs, c='green', linestyle='-')
plt.xlabel('Size')
plt.ylabel('Time')
plt.title('Smaller Sizes [1 - 100]')
plt.legend()
plt.show()
###
# Blue plot and the Red plot are overlapping due to the speed of the sorting
# algorithm with small arrays (size: 10, 100, 1000)
###
###
# Plot 4 placeholder
###
###
# new cell
###
import numpy as np
import matplotlib.pyplot as plt
ms1_times = lists[0]
ms2_times = lists[3]
ms3_times = lists[6]
ms_times = [ms1_times, ms2_times, ms3_times]
qs_times = [qs1_times, qs2_times, qs3_times]
hs_times = [hs1_times, hs2_times, hs3_times]
ms_avg = np.mean(ms_times, axis=0)
qs_avg = np.mean(qs_times, axis=0)
hs_avg = np.mean(hs_times, axis=0)
sizes = [1, 10, 100, 1000, 10000]
plt.figure(figsize=(10, 6)) # Width: 8 inches, Height: 6 inches
plt.plot(sizes, ms_avg, marker='o', markersize=4, alpha=1, label='Merge Sort')
plt.plot(sizes, qs_avg, marker='o', markersize=4, alpha=1, label='Quick Sort')
plt.plot(sizes, hs_avg, marker='o', markersize=4, alpha=1, label='Heap Sort')
plt.xlabel('Sizes')
plt.ylabel('Time')
plt.title('Averages of MS, QS, and HS')
plt.legend()
plt.show()
###
# Plot 5 placeholder
###
###
# new cell
###
import matplotlib.pyplot as plt
ms_1000 = ms1_times[2]
qs_1000 = qs1_times[2]
hs_1000 = hs1_times[2]
ms_10000 = ms1_times[3]
qs_10000 = qs1_times[3]
hs_10000 = hs1_times[3]
ms_100000 = ms1_times[4]
qs_100000 = qs1_times[4]
hs_100000 = hs1_times[4]
x = ['1000','10000', '1000000']
y_ms = [ms_1000, ms_10000, ms_100000]
y_qs = [qs_1000, qs_10000, qs_100000]
y_hs = [hs_1000, hs_10000, hs_100000]
plt.figure(figsize=(10, 6))
plt.scatter(x, y_hs, c='red', label='Heap Sort')
plt.plot(x, y_hs, c='red', linestyle='-')
plt.scatter(x, y_ms, c='blue', label='Merge Sort')
plt.plot(x, y_ms, c='blue', linestyle='-')
plt.scatter(x, y_qs, c='green', label='Quick Sort')
plt.plot(x, y_qs, c='green', linestyle='-')
plt.xlabel('Size')
plt.ylabel('Time')
plt.title('Bigger Sizes [1000 - 100000]')
plt.legend()
plt.show()
###
# Plot 6 placeholder
###
Data
- Note: If anyone is going to review this, please copy the following data into a
data.txt
since that is the file my script is looking for in the directory.
[time]:2
[time]:31
[time]:168
[time]:2015
[time]:22956
[time]: 2
[time]: 13
[time]: 186
[time]: 2125
[time]: 24799
[time]:1
[time]:15
[time]:246
[time]:3192
[time]:41653
[time]: 2
[time]: 14
[time]: 169
[time]: 2061
[time]: 23578
[time]: 2
[time]: 12
[time]: 160
[time]: 2218
[time]: 26630
[time]: 1
[time]: 17
[time]: 253
[time]: 3367
[time]: 42713
[time]: 2
[time]: 14
[time]: 163
[time]: 1980
[time]: 22682
[time]: 2
[time]: 12
[time]: 164
[time]: 2092
[time]: 25826
[time]: 1
[time]: 15
[time]: 245
[time]: 3253
[time]: 40700
I would love some feedback on potential improvements and design choice recommendations. I am going into Data Science
next year where the use of Jupyter Notebooks and Python is very common.