Skip to content

Commit

Permalink
Added simulation in bubble sort (#327)
Browse files Browse the repository at this point in the history
* Update bubble_sort.py

* Update bubble_sort.py

* Update bubble_sort.py

* Update bubble_sort.py
  • Loading branch information
geon0325 authored and goswami-rahul committed Jun 7, 2018
1 parent 69d4b7d commit 41dbd32
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion algorithms/sort/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@
Worst-case performance: O(N^2)
If you call bubble_sort(arr,True), you can see the process of the sort
Default is simulation = False
"""


def bubble_sort(arr):
def bubble_sort(arr, simulation=False):
def swap(i, j):
arr[i], arr[j] = arr[j], arr[i]

n = len(arr)
swapped = True

if simulation:
print(arr)

while swapped:
swapped = False
for i in range(1, n):
if arr[i - 1] > arr[i]:
swap(i - 1, i)
swapped = True
if simulation:
print(arr)

return arr

0 comments on commit 41dbd32

Please sign in to comment.