ppathak35

selection sort simulations

Jun 10th, 2022 (edited)
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. SCREEN = WIDTH, HEIGHT = 640, 480
  6.  
  7. info = pygame.display.Info()
  8. width = info.current_w
  9. height = info.current_h
  10.  
  11. if width >= height:
  12.     win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
  13. else:
  14.     win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
  15.  
  16. clock = pygame.time.Clock()
  17. FPS = 15
  18.  
  19. # COLORS **********************************************************************
  20.  
  21. WHITE = (255, 255, 255)
  22. BLUE = (30, 144,255)
  23. RED = (255, 0, 0)
  24. GREEN = (0, 205, 0)
  25. BLACK = (0, 0, 0)
  26.  
  27. colors = [WHITE, (200, 200, 200), (150, 150, 150)]
  28. colors2 = [GREEN, (34, 139, 34), (144, 238, 144)]
  29.  
  30. # Font ***********************************************************************
  31.  
  32. font = pygame.font.SysFont("Arial", 15)
  33. insertion_text = font.render("I: Insertion Sort", True, WHITE)
  34. bubble_text = font.render("B: Bubble Sort", True, WHITE)
  35. selection_text = font.render("S: Selection Sort", True, WHITE)
  36.  
  37. SIDEPAD = 40
  38. TOPPAD = 100
  39.  
  40. n = 100
  41. minh = 10
  42. maxh = 200
  43.  
  44. def getRandomHeights():
  45.     heights = []
  46.     for i in range(n):
  47.         ht = random.randint(minh, maxh)
  48.         heights.append(ht)
  49.     random.shuffle(heights)
  50.     return heights
  51. heights = getRandomHeights()
  52. width = (WIDTH - SIDEPAD) // len(heights)
  53. height = (HEIGHT - TOPPAD) // (max(heights) - min(heights))
  54.  
  55. sorting = False
  56.  
  57. def draw(arr, i, j):
  58.     for index, ht in enumerate(heights):
  59.             color = colors[index%3]
  60.             if index == i:
  61.                 color = BLUE
  62.             elif index == j:
  63.                 color = RED
  64.             elif index < i:
  65.                 color = colors2[index % 3]
  66.             pygame.draw.rect(win, color, ((20 + width*index), HEIGHT - (ht * height), width, height * ht) )
  67.  
  68. def selectionSort(arr):
  69.     for i in range(len(arr)):
  70.         min_idx = i
  71.         for j in range(i+1, len(arr)):
  72.             if arr[min_idx] > arr[j]:
  73.                 min_idx = j
  74.    
  75.         arr[i], arr[min_idx] = arr[min_idx], arr[i]
  76.         draw(arr, i, min_idx)
  77.        
  78.         yield True
  79.  
  80.     return arr
  81.    
  82. def bubbleSort(arr):
  83.     for i in range(len(arr)-1):
  84.         for j in range(len(arr) - i -1):
  85.             if arr[j] > arr[j+1]:
  86.                 m = j
  87.                 arr[j], arr[j+1] = arr[j+1], arr[j]
  88.                
  89.         draw(arr, m, m+1)
  90.         yield True
  91.                
  92.     # return arr
  93.     print(arr)
  94.  
  95. def insertionSort(arr):
  96.     for i in range(len(arr)):
  97.         key = arr[i]
  98.         j = i-1
  99.         while(j>=0 and arr[j] > key):
  100.             arr[j+1] = arr[j]
  101.             j -= 1
  102.         arr[j+1] = key
  103.         draw(arr, i, j)
  104.         yield True
  105.        
  106.     return arr
  107.  
  108. # GAME ************************************************************************
  109.  
  110. running = True
  111. while running:
  112.     win.fill(BLACK)
  113.     for event in pygame.event.get():
  114.         if event.type == pygame.QUIT:
  115.             running = False
  116.  
  117.         if event.type == pygame.KEYDOWN:
  118.             if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
  119.                 running = False
  120.  
  121.             if event.key == pygame.K_s and not sorting:
  122.                 key = selectionSort(heights)
  123.                 sorting = True
  124.                
  125.             if event.key == pygame.K_b and not sorting:
  126.                 key = bubbleSort(heights)
  127.                 sorting = True
  128.  
  129.             if event.key == pygame.K_i and not sorting:
  130.                 key = insertionSort(heights)
  131.                 sorting = True
  132.  
  133.             if event.key == pygame.K_r and not sorting:
  134.                 heights = getRandomHeights()
  135.                 width = (WIDTH - SIDEPAD) // len(heights)
  136.                 height = (HEIGHT - TOPPAD) // (max(heights) - min(heights))
  137.                 sorting = False
  138.  
  139.     n_text = font.render(f"n : {n}", True, WHITE)
  140.     minh_text = font.render(f"minh : {minh}", True, WHITE)
  141.     maxh_text = font.render(f"maxh : {maxh}", True, WHITE)
  142.  
  143.     win.blit(insertion_text, (20, 20))
  144.     win.blit(selection_text, (20, 50))
  145.     win.blit(bubble_text, (20, 80))
  146.     win.blit(n_text, (300, 20))
  147.     win.blit(minh_text, (400, 20))
  148.     win.blit(maxh_text, (500, 20))
  149.  
  150.     if sorting:
  151.             try:
  152.                 next(key)
  153.             except:
  154.                 sorting = False
  155.     else:
  156.         for index, ht in enumerate(heights):
  157.             color = colors[index % 3]
  158.             pygame.draw.rect(win, color, ((20 + width*index), HEIGHT - height * ht, width, height * ht) )
  159.  
  160.     clock.tick(FPS)
  161.     pygame.display.update()
  162.  
  163. pygame.quit()
Add Comment
Please, Sign In to add comment