LoginSignup
1
1

More than 3 years have passed since last update.

バブルソートでソートされる様子を可視化する

Posted at

概要

バブルソートでソートされる様子を matplotlib を使って描画してみました
※jupyter notebook上に貼り付けて実行してください

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()
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1