I tried to draw how it is sorted by bubble sort using matplotlib
bubble_sort.py
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
list_a = [5,7,4,5,1,2,3,2,9,1,4]
left = np.arange(1, len(list_a) + 1)
height = list_a
plt.bar(left, height)
plt.show()
for i in range(len(list_a)):
for j in range(0, len(list_a) - i - 1):
if list_a[j] > list_a[j + 1]:
list_a[j], list_a[j + 1] = list_a[j + 1], list_a[j]
height = np.array(list_a)
plt.bar(left, height)
plt.show()
Recommended Posts