Skip to content

Commit 0ce1e13

Browse files
committed
Bubble sort - short bubble
1 parent 85a454b commit 0ce1e13

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

sorting/bubble-sort.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
array = [54, 26, 93, 17, 77, 31, 44, 55, 20]
3+
4+
5+
def bubble_sort(nums: list):
6+
for i in range(len(nums)):
7+
for j in range(len(nums) - 1, i, -1):
8+
if nums[j - 1] > nums[j]:
9+
nums[j - 1], nums[j] = nums[j], nums[j - 1]
10+
11+
12+
print(array)
13+
bubble_sort(array)
14+
print(array)

sorting/short-bubble.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
array = [54, 26, 93, 17, 77, 31, 44, 55, 20]
3+
4+
5+
def short_bubble(nums: list):
6+
swapped: bool = True
7+
dec: int = 1
8+
while swapped:
9+
swapped = False
10+
for i in range(len(nums) - dec):
11+
if nums[i] > nums[i + 1]:
12+
nums[i + 1], nums[i] = nums[i], nums[i + 1]
13+
swapped = True
14+
dec += 1
15+
16+
17+
print(array)
18+
short_bubble(array)
19+
print(array)

0 commit comments

Comments
 (0)