-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.py
More file actions
32 lines (28 loc) · 876 Bytes
/
Copy pathInsertionSort.py
File metadata and controls
32 lines (28 loc) · 876 Bytes
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
import matplotlib.pyplot as plt
import matplotlib.animation as an
from DatasetGenerator import get_dataset
swaps = 0
fig, ax = plt.subplots()
ax.set_title('Insertion Sort')
numberOfSwaps = ax.text(0.01, 0.95, "", transform=ax.transAxes)
array = get_dataset()
bars = ax.bar(range(len(array)), array, color='black')
def insertionSort(array):
for i in range(1, len(array)):
key = array[i]
j = i-1
while j >= 0 and array[j] > key:
array[j+1] = array[j]
j -= 1
yield array
array[j+1] = key
def update(array, bars):
global swaps
for bar, val in zip(bars, array):
bar.set_height(val)
swaps += 1
numberOfSwaps.set_text("Swaps: {}".format(swaps))
anim = an.FuncAnimation(fig, func=update,
fargs=(bars, ), frames=insertionSort(array), interval=1,
repeat=False)
plt.show()