Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Python 2.7 Bubble-Sort Visualization By Lucid Potato
- import pygame
- from random import randint
- import winsound #Remove if on Linux
- pygame.init()
- size = (500,500)
- window = pygame.display.set_mode((size))
- pygame.display.set_caption("Pygame Bubble-Sort Visualization")
- black = pygame.Color(0,0,0)
- white = pygame.Color(255,255,255)
- heightList = []
- listLength = 100
- xList,y,w = [],0,5
- tmpX = 0
- for i in range(listLength):
- heightList.append(randint(0,500))
- xList.append(tmpX)
- tmpX += w
- def draw():
- global xList,y,heightList
- for i in range(listLength):
- pygame.draw.rect(window,white,(xList[i],y,w,heightList[i]),0)
- isSorted = False
- sortedCount = 0
- trackInt = 0
- while True:
- window.fill(black)
- if trackInt == listLength-1:
- trackInt = 0
- if sortedCount > listLength and isSorted == False:
- print("Sorted!")
- print(heightList)
- isSorted = True
- if heightList[trackInt] > heightList[trackInt+1] and isSorted == False:
- tmp = heightList[trackInt+1]
- heightList[trackInt+1],heightList[trackInt] = heightList[trackInt],tmp
- sortedCount = 0
- trackInt += 1
- winsound.Beep(heightList[trackInt] + 2000,10) #Remove if on Linux
- else:
- sortedCount += 1
- trackInt += 1
- winsound.Beep(1000,5) #Remove if on Linux
- draw()
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment