PHC-MakiBoT
Forum Guru
- Joined
- Jan 10, 2018
- Posts
- 1,022
- Reaction
- 8,003
- Points
- 1,079
Guyz Share ko lang (SKL)
Sorting Visualizer with Bubble Sort Algorithm
83 lines lang poo ng code
Pahingi na din po ng mga ideas for python or pygame coding ,
Note:
Install nyo muna yung pygame: pip install pygame
====================================================================
====================================================================
Sa mga nahihilo sa copy/paste iDownload nyo nalang yung file na naka attach
hope you like it po
Sorting Visualizer with Bubble Sort Algorithm
83 lines lang poo ng code
Pahingi na din po ng mga ideas for python or pygame coding ,
Note:
Install nyo muna yung pygame: pip install pygame
====================================================================
Code:
import pygame
from random import *
import time
import os
pygame.init ()
window = pygame.display.set_mode((720,480))
pygame.display.set_caption("Sorting Visualizer || Bubble Sort Algorithm || Version 1.0")
white = (255,255,255)
black = (0,0,0)
green = ((0,255,0))
red = ((255,0,0))
blue = (0, 0, 128)
X = 50
Y = 50
run = True
data_arr = [0] * 72
sort = False
def gen_seed():
arr_height = []
for _ in range(72):
value = randint(1,480)
arr_height.append(value)
return arr_height
def draw_seed(arr_data):
offset = 0
i = 0
for _ in range(72):
pygame.draw.rect(window, black, [offset,480,10,arr_data[i] * -1])
i += 1
offset += 10
return arr_data
def bubble_sort(data):
for j in range(len(data)-1):
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]
def drawStatus(isSort):
if isSort:
font1 = pygame.font.Font('freesansbold.ttf', 20)
text1 = font1.render('Hit "ENTER" to Stop Bubble Sort', True, green, blue)
textRect1 = text1.get_rect()
textRect1.center = (X // 2 * 7, Y // 2)
window.blit(text1, textRect1)
font4 = pygame.font.Font('freesansbold.ttf', 20)
text4 = font4.render('Stop the bubble sort to before generate new!', True, green, blue)
textRect4 = text4.get_rect()
textRect4.center = (X // 2 * 9, Y // 2 * 2)
window.blit(text4, textRect4)
else:
font2 = pygame.font.Font('freesansbold.ttf', 20)
text2 = font2.render('Hit "SPACE" to generate', True, green, blue)
textRect2 = text2.get_rect()
textRect2.center = (X // 2 * 6, Y // 2 * 2)
window.blit(text2, textRect2)
font3 = pygame.font.Font('freesansbold.ttf', 20)
text3 = font3.render('Hit "ENTER" to Start Bubble Sort', True, green, blue)
textRect3 = text3.get_rect()
textRect3.center = (X // 2 * 7, Y // 2)
window.blit(text3, textRect3)
while run:
time.sleep(0.05)
window.fill(white)
drawStatus(sort)
if sort:
bubble_sort(data_arr)
draw_seed(data_arr)
else:
draw_seed(data_arr)
for event in pygame.event.get():
if (event.type == pygame.QUIT):
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if sort:
print("Sorting Status: ON : Hit ENTER to turn OFF")
else:
data_arr = gen_seed()
if event.key == pygame.K_RETURN:
if sort:
sort = False
else:
sort = True
pygame.display.update()
pygame.quit()
====================================================================
Sa mga nahihilo sa copy/paste iDownload nyo nalang yung file na naka attach
hope you like it po
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.
Last edited: