-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSortCasesPlot.py
More file actions
48 lines (41 loc) · 1.25 KB
/
insertionSortCasesPlot.py
File metadata and controls
48 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import random
from time import time
from sorting import insertionSort
import matplotlib.pyplot as plt
n=1000
insertionTimeArrayRandom = []
insertionTimeArrayBest = []
insertionTimeArrayWorst = []
sizeArr =[]
for i in range(n,n*11,n):
sizeArr.append(i)
randomValues = random.sample(range(i), i)
bestCase = randomValues
worstCase = randomValues
startTime = time()
insertionSort(randomValues)
endTime = time()
totalTime = endTime -startTime
insertionTimeArrayRandom.append(totalTime)
print("For",i,"the time is",totalTime)
bestCase.sort()
startTime = time()
insertionSort(bestCase)
endTime = time()
totalTime = endTime -startTime
insertionTimeArrayBest.append(totalTime)
print("For",i,"the time is",totalTime)
worstCase.sort(reverse = True)
startTime = time()
insertionSort(worstCase)
endTime = time()
totalTime = endTime -startTime
insertionTimeArrayWorst.append(totalTime)
print("For",i,"the time is",totalTime)
# Plot size vs time graph
fig, ax = plt.subplots(1, 1)
ax.plot(sizeArr,insertionTimeArrayRandom, label = 'InsertionSortRandom')
ax.plot(sizeArr,insertionTimeArrayBest, label = 'InsertionSortBest')
ax.plot(sizeArr,insertionTimeArrayWorst, label = 'InsertionSortWorst')
legend = ax.legend(loc = 'upper center', fontsize = 'large')
plt.show()