[PYTHON] Visualize how it is sorted by bubble sort

Overview

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

Visualize how it is sorted by bubble sort
Bubble sort
Bubble sort
I tried to program bubble sort by language
[Python] How to sort instances by instance variables
Python> Sort by number and sort by alphabet> Use sorted ()
Sort by pandas
visualize insertion sort
What is pip and how do you use it?