Skip to content

Commit 1ac0eec

Browse files
authored
feat: add change signal in bubble sort python program (doocs#1047)
1 parent c5e9483 commit 1ac0eec

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

basic/sorting/BubbleSort/BubbleSort.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
def bubbleSort(arr):
2+
hasChange = True
23
n = len(arr)
34
# Iterate over all array elements
45
for i in range(n):
6+
if not hasChange:
7+
break
8+
9+
hasChange = False
510
# Last i elements are already in place
611
for j in range(n - i - 1):
712
if arr[j] > arr[j + 1]:
813
arr[j], arr[j + 1] = arr[j + 1], arr[j]
14+
hasChange = True
915

1016

1117
arr = [64, 34, 25, 12, 22, 11, 90]

0 commit comments

Comments
 (0)