Guest User

Untitled

a guest
Sep 23rd, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. import random
  3. import matplotlib.animation as animation
  4. import time
  5.  
  6. def bubbleSort(list):
  7.     global values
  8.  
  9.     n = len(list)
  10.     elementsInPlace = 0
  11.     comparisonCount = 0
  12.    
  13.  
  14.     while n > 1:
  15.         for i in range(len(list) - elementsInPlace - 1):
  16.             if list[i] > list[i + 1]:
  17.                 comparisonCount += 1
  18.                 list[i], list[i + 1] = list[i + 1], list[i]
  19.                
  20.                 updateGraph(list)
  21.  
  22.  
  23.  
  24.             else:
  25.                 comparisonCount += 1
  26.  
  27.  
  28.         n -= 1
  29.         elementsInPlace += 1
  30.    
  31.  
  32.     return list
  33.  
  34.  
  35. size = input("How big do you want the array to be?")
  36.  
  37. randomlist = random.sample(range(1, int(size) + 1), int(size))
  38.  
  39. plt.ylim((0, int(size)))
  40. plt.xlim((0, int(size)))
  41.  
  42.  
  43. plt.title("Bubble Sort Visualization")
  44.  
  45. plt.show()
  46.  
  47. bubbleSort(randomlist)
  48.  
  49.  
  50. def updateGraph(list):
  51.     plt.bar(range(0, int(size)), list)
  52.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment