我想制作 insertig 排序可视化工具,排序有效但可视化工具无效

I want to make insertig sorting visualizer,the sorting works but not the visualizer

正如标题所说,我想用 python 和 pygame 创建一个可视化的插入排序。排序工作完美,但在可视化时它永远不会得到正确的输出。

排序完美,但屏幕栏没有改变它们的位置。我还添加了一些颜色以更好地理解代码..

代码:-

import pygame
import random

pygame.init()
screen = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("SORTING VISUALS")


class Rect:
    def __init__(self, x, y, width, height):
        self.X = x
        self.Y = y
        self.width = width
        self.height = height
        self.colour = BLACK

    def show(self):
        pygame.draw.rect(screen, self.colour, (self.X, self.Y, self.width, self.height))

    def changeCol(self, colour):
        self.colour = colour


WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
GRAY = (148, 0, 211)

L = []
rect = []
n = 100  # n belongs till [50,220]
WidthOfEachBar = 800 // (n + 1)


def array(n_):
    global L
    arr = [(3 * i) for i in range(1, n_ + 1)]
    for a in range(n_):
        random_no = random.choice(arr)
        L.append(random_no + 10)
        arr.remove(random_no)


array(n)

for i in range(n):
    x = (i + 1) * (1 + WidthOfEachBar)
    y = 680 - L[i]
    rect.append(Rect(x, y, WidthOfEachBar, L[i]))


# /*-------------------------------------- INSERTION SORT FUNCTION----------------------------------------------------*/
def equal(b):
    a = Rect(0, 0, 0, 0)
    a.X = b.X
    a.Y = b.Y
    a.width = b.width
    a.height = b.height
    a.colour = b.colour
    return a


def insertionsort():
    global rect, n
    for oo in range(n):
        print(rect[oo].height, end=" ")
    print()
    for i in range(1, n):
        print("outer loop")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        rect[i].colour = GREEN
        key = rect[i]
        key.X = rect[i].X
        j = i - 1
        while j >= 0 and key.height < rect[j].height:
            for oo in range(n):
                print(rect[oo].height, end=" ")
            print()
            rect[j + 1] = rect[j]
            rect[j + 1].X = rect[j].X
            j = j - 1

        rect[j + 1] = key
        rect[j + 1].X = key.X

        screen.fill(WHITE)
        for a in range(n):
            rect[a].show()
        pygame.display.update()
        rect[j+1].colour = BLACK


sorted_insertion = False
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
    if not sorted_insertion:
        insertionsort()
        for oo in range(n):
            print(rect[oo].height, end=" ")
        print()
        print("came once")
        sorted_insertion = True
        for a in range(n):
            rect[a].show()
        pygame.display.update()

这里的画面每次都会更新,但是我没看懂问题所在。代码很可疑,我知道,但任何帮助都会很棒...

您必须交换列表元素和 .X 属性,而不是赋值,如果:

rect[j + 1] = rect[j]
rect[j + 1].X = rect[j].X

rect[j + 1], rect[j] = rect[j], rect[j + 1]
rect[j + 1].X, rect[j].X = rect[j].X, rect[j + 1].X 

函数insertionsort:

def insertionsort():
    global rect, n
    for oo in range(n):
        print(rect[oo].height, end=" ")
    print()
    for i in range(1, n):
        print("outer loop")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        rect[i].colour = GREEN
        key = rect[i]
        key.X = rect[i].X
        j = i - 1
        while j >= 0 and key.height < rect[j].height:
            for oo in range(n):
                print(rect[oo].height, end=" ")
            print()
            rect[j + 1], rect[j] = rect[j], rect[j + 1]
            rect[j + 1].X, rect[j].X = rect[j].X, rect[j + 1].X
            j = j - 1

        rect[j + 1], key = key, rect[j + 1]
        rect[j + 1].X, key.X = key.X, rect[j + 1].X

        screen.fill(WHITE)
        for a in range(n):
            rect[a].show()
        pygame.display.update()
        rect[j+1].colour = BLACK