猫史档案馆


排序算法可视化

用户:蒟蒻OIer1048576蒟蒻OIer1048576查看:2 回复:3 评论:2 创建时间:2020-07-14T09:14:55


import turtle
import random
import time
import threading
lst = [random.randrange(1,800) for i in range(20)]
colors = ["red","orange","yellow","green","skyblue","blue","purple"]
turtle.speed('fastest')
def fun(i,j,color=None):
    if color is None:
        turtle.color(colors[i%7])
    else:
        turtle.color(color)
    turtle.pu()
    turtle.goto(i*30-300,-400)
    turtle.pd()
    turtle.begin_fill()
    turtle.sety(j-400)
    turtle.setx(i*30-270)
    turtle.sety(-400)
    turtle.setx(i*30-300)
    turtle.end_fill()
a = lst
b = len(a) - 1
counter = 0
while b:
    for m in range(b):
        time.sleep(0.2)
        turtle.clear()
        n = m + 1
        if a[m] > a[n]:
            a[m], a[n] = a[n], a[m]     
        for i,j in enumerate(a):
            if i in [m,n]:
                fun(i,j,"pink")
            elif len(a)-i <= counter:
                fun(i,j,"gray")
            else:
                fun(i,j)
    b -= 1
    counter += 1


turtle.mainloop()


回复

上一页1 页 / 共 1下一页
蒟蒻OIer1048576蒟蒻OIer1048576

顶帖

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

dd

点赞1


评论


蒟蒻OIer1048576蒟蒻OIer1048576

点赞0


评论