Repository navigation
Expand file tree
/
Copy pathbubble_sort.py
More file actions
56 lines (40 loc) · 1.46 KB
/
Copy pathbubble_sort.py
File metadata and controls
56 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Bubble Sort Visualization
This Python script demonstrates the Bubble Sort algorithm using matplotlib to visualize the sorting process.
Usage:
- The script generates a random list of integers for demonstration purposes.
- The algorithm sorts the list in ascending order, providing a step-by-step visualization.
Dependencies:
- matplotlib: A data visualization library for creating static, animated, and interactive visualizations in Python.
Author: Karthik
Date: 12-12-2023
"""
import matplotlib.pyplot as plt
import numpy as np
def bubble_sort_visualization(lst):
"""
Sorts a list using the Bubble Sort algorithm and visualizes each step.
Parameters:
- lst (list): The input list to be sorted.
Returns:
None
"""
amount = len(lst)
x = np.arange(0, amount, 1)
# Iterate through each element in the list
for i in range(amount):
# Compare and swap adjacent elements to bubble up the largest element
for j in range(0, amount-i-1):
# Visualize the current state of the list using a bar chart
plt.bar(x, lst)
plt.pause(0.001)
plt.clf()
# Swap if the element at position j is greater than the next element
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
# Display the final sorted list
plt.show()
# Example usage
amount = 15
lst_to_sort = np.random.randint(0, 100, amount)
bubble_sort_visualization(lst_to_sort)