diff --git a/algorithms/sort/bubble_sort.py b/algorithms/sort/bubble_sort.py index 0a8a47063..63c6c4f14 100644 --- a/algorithms/sort/bubble_sort.py +++ b/algorithms/sort/bubble_sort.py @@ -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