Skip to content

Commit abd0f82

Browse files
Created Bubble Sort
1 parent 28421ce commit abd0f82

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

Sorting Algorithims/Bubble sort

Lines changed: 0 additions & 1 deletion
This file was deleted.

Sorting Algorithims/Bubble_sort.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def bubble_sort(nums):
2+
# We set swapped to True so the loop looks runs at least once
3+
swapped = True
4+
while swapped:
5+
swapped = False
6+
for i in range(len(nums) - 1):
7+
if nums[i] > nums[i + 1]:
8+
# Swap the elements
9+
nums[i], nums[i + 1] = nums[i + 1], nums[i]
10+
# Set the flag to True so we'll loop again
11+
swapped = True
12+
13+
14+
# Verify it works
15+
random_list_of_nums = [5, 2, 1, 8, 4]
16+
bubble_sort(random_list_of_nums)
17+
print(random_list_of_nums)

0 commit comments

Comments
 (0)