-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_visualization.py
More file actions
33 lines (27 loc) · 860 Bytes
/
sorting_visualization.py
File metadata and controls
33 lines (27 loc) · 860 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
33
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
def bubble_sort(data):
n=len(data)
for i in range(n):
for j in range(0,n-i-1):
if data[j]>data[j+1]:
data[j],data[j+1]=data[j+1],data[j]
yield data.copy()
def generate_data(size):
return [random.randint(1,100) for _ in range(size)]
def update_plot(frames,bars):
for bar, val in zip(bars, frames):
bar.set_height(val)
return bars
def main():
size=30
data=generate_data(size)
fig, ax=plt.subplots()
bars=ax.bar(range(len(data)),data,color='blue')
ax.set_xlim(0,len(data))
ax.set_ylim(0,max(data)+10)
ani=animation.FuncAnimation(fig,update_plot,fargs=(bars,),frames=bubble_sort(data),repeat=False,interval=50)
plt.show()
if __name__=="__main__":
main()