You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Turtle排序可视化工具:插入排序可视化位置移动异常

Python Turtle插入排序可视化位置错误问题

我开发了一个基于Python Turtle的排序可视化工具,其中ins_sort()函数需完成以下三个任务:

  • 对存储Turtle对象的列表a按升序排序
  • 对对应长度的列表l按升序排序
  • 将线条(Turtle对象)移动到屏幕上正确的排序位置(实现可视化)

目前ins_sort()能正确排序列表a和l,但可视化环节存在问题——无法将线条移动到正确位置。尝试过ChatGPT、查阅文档等方法仍未解决,怀疑可能和Turtle模块或内置函数有关。

问题代码(ins_sort()函数)

# Insertion Sort
def ins_sort():
    for i in range(1, len(l)):
        key = l[i]
        key_turtle = a[i]
        key_turtle.color("red")
        j = i - 1
        while j >= 0 and key < l[j]:
            a[j].color("yellow")
            a[j].setx(a[j + 1].xcor())
            a[j + 1] = a[j]
            l[j + 1] = l[j]
            j -= 1
            # wn.update()
        key_turtle.setx(a[j + 1].xcor())
        a[j + 1] = key_turtle
        l[j + 1] = key
        a[j + 1].color("green")
        wn.update()

完整项目代码

import turtle
import random
import time

# Screen
wn = turtle.Screen()
wn.title("Sorting Visualiser")
wn.setup(width=1200, height=700)
wn.tracer(0)


# Background
def display_vis():
    dispbg = turtle.Turtle()
    dispbg.color("grey")
    dispbg.fillcolor("#DAECFF")
    dispbg.speed(0)
    dispbg.hideturtle()
    dispbg.penup()
    dispbg.width(4)
    dispbg.goto(-412.5, 266)
    dispbg.pendown()
    dispbg.begin_fill()
    for i in range(2):
        dispbg.forward(831.5)
        dispbg.right(90)
        dispbg.forward(569)
        dispbg.right(90)
    dispbg.end_fill()
    wn.update()


# Initialization
a = []
l = []
d = 8
pen = [0] * 4
writer = [0] * 4

write_time = turtle.Turtle()
write_time.hideturtle()

# Hiding Turtle
def hide_turtle():
    for i in range(4):
        pen[i].reset()
        pen[i].hideturtle()
        writer[i].reset()
        writer[i].hideturtle()
    wn.update()


# Showing Turtle
def show_turtle():
    function_button()


# Generating Lines
def generate_lines():
    global d, a, l
    for _ in range(100):
        line = turtle.Turtle()
        line.hideturtle()
        line.shape("square")
        line.color("cyan")
        alen = random.uniform(0.1, 28)
        line.shapesize(stretch_len=alen, stretch_wid=0.2)
        l.append(alen)
        line.speed(0)
        line.left(90)
        line.penup()
        res_len_sel = 300 - (alen * 10)
        line.goto((-400 + d), -res_len_sel)
        line.showturtle()
        a.append(line)
        d += 8
    d = 8
    wn.update()

# Selection Sort
def sel_sort():
    for i in range(100):
        a[i].color("#FF1100")
        var = a[i].xcor()
        length = l[i]
        pos = i

        for j in range(i + 1, 100):
            if l[j] < length:
                length = l[j]
                var = a[j].xcor()
                pos = j
            a[j].color("cyan")

        a[pos].color("green")
        a[pos].setx(a[i].xcor())
        a[i].setx(var)
        a[i], a[pos] = a[pos], a[i]
        l[i], l[pos] = l[pos], l[i]
        wn.update()

# Bubble Sort
def bub_sort():
    for i in range(99):
        for j in range(99 - i):
            var = a[j].xcor()
            vara = a[j + 1].xcor()
            if l[j] > l[j + 1]:
                a[j].setx(vara)
                a[j + 1].setx(var)
                a[j], a[j + 1] = a[j + 1], a[j]
                l[j], l[j + 1] = l[j + 1], l[j]
                a[j].color("cyan")
            a[j + 1].color("green")
        wn.update()
    a[0].color("green")
    wn.update()

# Insertion Sort
def ins_sort():
    for i in range(1, len(l)):
        key = l[i]
        key_turtle = a[i]
        key_turtle.color("red")
        j = i - 1
        while j >= 0 and key < l[j]:
            a[j].color("yellow")
            a[j].setx(a[j + 1].xcor())
            a[j + 1] = a[j]
            l[j + 1] = l[j]
            j -= 1
            # wn.update()
        key_turtle.setx(a[j + 1].xcor())
        a[j + 1] = key_turtle
        l[j + 1] = key
        a[j + 1].color("green")
        wn.update()

# Clearing Screen
def clear_screen():
    global a, l
    for line in a:
        line.clear()
        line.hideturtle()
    del a[:]
    del l[:]
    generate_lines()

# Generating Buttons
def generate_buttons(x, y, obj, msg):
    pen[obj] = turtle.Turtle()
    pen[obj].hideturtle()
    pen[obj].penup()
    pen[obj].goto(x - 80, y - 20)
    pen[obj].pendown()
    pen[obj].color("#52A1FA")
    pen[obj].begin_fill()
    for i in range(2):
        pen[obj].fd(160)
        pen[obj].left(90)
        pen[obj].fd(40)
        pen[obj].left(90)
    pen[obj].end_fill()
    writer[obj] = turtle.Turtle()
    writer[obj].hideturtle()
    writer[obj].penup()
    writer[obj].goto(x, y - 7.5)
    writer[obj].write(msg, font=('Verdana', 12, 'normal'), align="center")
    wn.update()

# Generating Buttons
def function_button():
    generate_buttons(-300, 300, 0, "Create New Lines")
    generate_buttons(-100, 300, 1, "Selection Sort")
    generate_buttons(100, 300, 2, "Bubble Sort")
    generate_buttons(300, 300, 3, "Insertion Sort")

# Time Taken
def write_timeTaken(t):
    global write_time
    write_time.hideturtle()
    write_time.penup()
    write_time.goto(0, -325)
    write_time.write("Time Taken: " + str(t), font=('Verdana', 12, 'normal'), align="center")
    wn.update()

# Checking Screen Click
def checki(x, y):
    write_time.clear()
    if x > (-380) and x < (-220) and y > 280 and y < 320:
        hide_turtle()
        clear_screen()
        show_turtle()
    elif x > (-180) and x < (-20) and y > 280 and y < 320:
        hide_turtle()
        t1 = time.time()
        sel_sort()
        t2 = time.time()
        write_timeTaken(t2 - t1)
        show_turtle()
    elif x > 20 and x < 180 and y > 280 and y < 320:
        hide_turtle()
        t1 = time.time()
        bub_sort()
        t2 = time.time()
        write_timeTaken(t2 - t1)
        show_turtle()
    elif x > 220 and x < 380 and y > 280 and y < 320:
        hide_turtle()
        t1 = time.time()
        ins_sort()
        t2 = time.time()
        write_timeTaken(t2 - t1)
        show_turtle()

# Screen
display_vis()

# Generating Buttons
function_button()
turtle.onscreenclick(checki)
turtle.listen()

turtle.done()

内容的提问来源于stack exchange,提问作者Md Aafaque Tabish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:58:09