diff --git a/code_to_optimize/bubble_sort_codeflash_trace.py b/code_to_optimize/bubble_sort_codeflash_trace.py index 48e9a412b..61a727727 100644 --- a/code_to_optimize/bubble_sort_codeflash_trace.py +++ b/code_to_optimize/bubble_sort_codeflash_trace.py @@ -1,14 +1,19 @@ from codeflash.benchmarking.codeflash_trace import codeflash_trace + + @codeflash_trace def sorter(arr): for i in range(len(arr)): - for j in range(len(arr) - 1): + swapped = False + for j in range(len(arr) - 1 - i): if arr[j] > arr[j + 1]: - temp = arr[j] - arr[j] = arr[j + 1] - arr[j + 1] = temp + arr[j], arr[j + 1] = arr[j + 1], arr[j] + swapped = True + if not swapped: + break return arr + @codeflash_trace def recursive_bubble_sort(arr, n=None): # Initialize n if not provided @@ -27,10 +32,12 @@ def recursive_bubble_sort(arr, n=None): # Recursively sort the remaining n-1 elements return recursive_bubble_sort(arr, n - 1) + class Sorter: @codeflash_trace def __init__(self, arr): self.arr = arr + @codeflash_trace def sorter(self, multiplier): for i in range(len(self.arr)):