-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.py
More file actions
19 lines (18 loc) · 841 Bytes
/
bubble_sort.py
File metadata and controls
19 lines (18 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def bubble_sort(data_list):
"""
Sorts a list using the Bubble Sort algorithm by performing adjacent permutations.
"""
n = len(data_list)
# Outer loop defines the number of passes through the list
for i in range(n - 1):
# Inner loop performs comparisons, '- i' avoids checking already sorted elements
for j in range(0, n - i - 1):
# If the current element is greater than the next, they are swapped
if data_list[j] > data_list[j + 1]:
data_list[j], data_list[j + 1] = data_list[j + 1], data_list[j]
# Testing the implementation
if __name__ == "__main__":
test_list = [1, 2, 3, 6, 23, 456, 57, 4, 5, 6, 46, 21, 12, 79, 7, 8, 9, 10]
print(f"Original list (Bubble): {test_list}")
bubble_sort(test_list)
print(f"Sorted list (Bubble): {test_list}")