Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- import random
- import matplotlib.animation as animation
- import time
- def bubbleSort(list):
- global values
- n = len(list)
- elementsInPlace = 0
- comparisonCount = 0
- while n > 1:
- for i in range(len(list) - elementsInPlace - 1):
- if list[i] > list[i + 1]:
- comparisonCount += 1
- list[i], list[i + 1] = list[i + 1], list[i]
- updateGraph(list)
- else:
- comparisonCount += 1
- n -= 1
- elementsInPlace += 1
- return list
- size = input("How big do you want the array to be?")
- randomlist = random.sample(range(1, int(size) + 1), int(size))
- plt.ylim((0, int(size)))
- plt.xlim((0, int(size)))
- plt.title("Bubble Sort Visualization")
- plt.show()
- bubbleSort(randomlist)
- def updateGraph(list):
- plt.bar(range(0, int(size)), list)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment