Search code examples
pythonpygamebubble-sort

I want to make a sorting Visualizer, the sort works but visualizer doesnot


Like the title says I wanted to create a visualized bubble sort with python and pygame. The sort works perfectly but when it comes to visualize it it never gets the correct output.

The sort works perfectly,but the screen bars donot get swapped. I also added some colours to better understand the code...

The Code:-

import pygame
import random

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)

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


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


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 = 50 + (i + 1) * (1 + WidthOfEachBar)
    y = 680 - L[i]
    rect.append(Rect(x, y, WidthOfEachBar, L[i]))


def swap(a, b):
    global rect
    rect[a], rect[b] = rect[b], rect[a]


def bubble_sort():
    global n, rect
    for ii1 in range(n):
        for j in range(n - 1):
            rect[j].colour = GREEN
            rect[j + 1].colour = GREEN

            if rect[j].height > rect[j + 1].height:
                # print(r[j].X, r[j + 1].X)
                swap(j, j + 1)
                # print(r[j].X, r[j + 1].X)
                for amb in range(n):
                    print(rect[amb].height, end=" ")
                print()
            screen.fill(WHITE)
            for no1 in range(n):
                rect[no1].show()
            pygame.time.delay(0)
            pygame.display.update()
            rect[j].colour = BLACK
            rect[j + 1].colour = BLACK


pygame.init()
screen = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("SORTING VISUALS")
is_sorted = False
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill(WHITE)
    if not is_sorted:
        bubble_sort()
    pygame.display.update()

Here the screen is updated everytime, but I couldn't understand the problem. The code is much fishy,I know but any help would be great


Solution

  • The problem in your code is that you're swapping the elements in the list, but the corresponding rectangles are not changing their position. You should change your swap function:

    def swap(a, b):
        global rect
        rect[a], rect[b] = rect[b], rect[a]
        rect[a].X, rect[b].X = rect[b].X, rect[a].X
    

    This will now swap the elements in the list as well as swap the positions of their corresponding rectangles.