Created
March 11, 2022 10:17
-
-
Save Eligijus112/ab4da38bec9cc92402d2c872285f5ced to your computer and use it in GitHub Desktop.
Bubble sort experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Maximum number of array length | |
| _max_length = 50 | |
| # Defining the amount of expiremnts in each test | |
| _n_experiments = 200 | |
| # Iteration results | |
| _iter_results = [] | |
| for i in range(_max_length + 1): | |
| for _ in range(_n_experiments): | |
| if i >= 2: | |
| # Creating a random array of length i | |
| _arr = [ | |
| int(random.random() * 100) for _ in range(i) | |
| ] | |
| # Creating the object | |
| _obj = BubbleSort(_arr) | |
| # Sorting | |
| _obj.sort() | |
| # Creating the results dictionary | |
| _results = { | |
| 'array_length': len(_arr), | |
| 'n_iter': _obj.n_iter, | |
| } | |
| # Appending to the master list | |
| _iter_results.append(_results) | |
| # Creating a dataframe for result visualization | |
| d = pd.DataFrame(_iter_results) | |
| # Ploting the boxplots per group | |
| plt.figure(figsize=(16, 12)) | |
| sns.boxplot(x='array_length', y='n_iter', data=d) | |
| plt.grid() | |
| plt.xlabel('Length of array') | |
| plt.ylabel('Number of iteration distribution') | |
| plt.title("Bubble Sort's iterations vs array length") | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment