Guest User

Python 2.7 Bubble-Sort Visualization with pygame

a guest
Sep 27th, 2015
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #Python 2.7 Bubble-Sort Visualization By Lucid Potato
  2. import pygame
  3. from random import randint
  4. import winsound #Remove if on Linux
  5. pygame.init()
  6. size = (500,500)
  7. window = pygame.display.set_mode((size))
  8. pygame.display.set_caption("Pygame Bubble-Sort Visualization")
  9. black = pygame.Color(0,0,0)
  10. white = pygame.Color(255,255,255)
  11.  
  12. heightList = []
  13. listLength = 100
  14. xList,y,w = [],0,5
  15. tmpX = 0
  16. for i in range(listLength):
  17.     heightList.append(randint(0,500))
  18.     xList.append(tmpX)
  19.     tmpX += w
  20. def draw():
  21.     global xList,y,heightList
  22.     for i in range(listLength):
  23.         pygame.draw.rect(window,white,(xList[i],y,w,heightList[i]),0)
  24.  
  25. isSorted = False
  26. sortedCount = 0
  27. trackInt = 0
  28. while True:
  29.     window.fill(black)
  30.     if trackInt == listLength-1:
  31.         trackInt = 0
  32.     if sortedCount > listLength and isSorted == False:
  33.         print("Sorted!")
  34.         print(heightList)
  35.         isSorted = True
  36.     if heightList[trackInt] > heightList[trackInt+1] and isSorted == False:
  37.         tmp = heightList[trackInt+1]
  38.         heightList[trackInt+1],heightList[trackInt] = heightList[trackInt],tmp
  39.         sortedCount = 0
  40.         trackInt += 1
  41.         winsound.Beep(heightList[trackInt] + 2000,10) #Remove if on Linux
  42.     else:
  43.         sortedCount += 1
  44.         trackInt += 1
  45.         winsound.Beep(1000,5) #Remove if on Linux
  46.     draw()
  47.     pygame.display.update()
  48.     for event in pygame.event.get():
  49.         if event.type == pygame.QUIT:
  50.             pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment