Skip to content

Commit 41dbd32

Browse files
geon0325goswami-rahul
authored andcommitted
Added simulation in bubble sort (keon#327)
* Update bubble_sort.py * Update bubble_sort.py * Update bubble_sort.py * Update bubble_sort.py
1 parent 69d4b7d commit 41dbd32

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

algorithms/sort/bubble_sort.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@
44
55
Worst-case performance: O(N^2)
66
7+
If you call bubble_sort(arr,True), you can see the process of the sort
8+
Default is simulation = False
9+
710
"""
811

912

10-
def bubble_sort(arr):
13+
def bubble_sort(arr, simulation=False):
1114
def swap(i, j):
1215
arr[i], arr[j] = arr[j], arr[i]
1316

1417
n = len(arr)
1518
swapped = True
19+
20+
if simulation:
21+
print(arr)
22+
1623
while swapped:
1724
swapped = False
1825
for i in range(1, n):
1926
if arr[i - 1] > arr[i]:
2027
swap(i - 1, i)
2128
swapped = True
29+
if simulation:
30+
print(arr)
31+
2232
return arr

0 commit comments

Comments
 (0)