|
| 1 | +from codeflash.benchmarking.codeflash_trace import codeflash_trace |
| 2 | +@codeflash_trace |
| 3 | +def sorter(arr): |
| 4 | + for i in range(len(arr)): |
| 5 | + for j in range(len(arr) - 1): |
| 6 | + if arr[j] > arr[j + 1]: |
| 7 | + temp = arr[j] |
| 8 | + arr[j] = arr[j + 1] |
| 9 | + arr[j + 1] = temp |
| 10 | + return arr |
| 11 | + |
| 12 | +@codeflash_trace |
| 13 | +def recursive_bubble_sort(arr, n=None): |
| 14 | + # Initialize n if not provided |
| 15 | + if n is None: |
| 16 | + n = len(arr) |
| 17 | + |
| 18 | + # Base case: if n is 1, the array is already sorted |
| 19 | + if n == 1: |
| 20 | + return arr |
| 21 | + |
| 22 | + # One pass of bubble sort - move the largest element to the end |
| 23 | + for i in range(n - 1): |
| 24 | + if arr[i] > arr[i + 1]: |
| 25 | + arr[i], arr[i + 1] = arr[i + 1], arr[i] |
| 26 | + |
| 27 | + # Recursively sort the remaining n-1 elements |
| 28 | + return recursive_bubble_sort(arr, n - 1) |
| 29 | + |
| 30 | +class Sorter: |
| 31 | + @codeflash_trace |
| 32 | + def __init__(self, arr): |
| 33 | + self.arr = arr |
| 34 | + @codeflash_trace |
| 35 | + def sorter(self, multiplier): |
| 36 | + for i in range(len(self.arr)): |
| 37 | + for j in range(len(self.arr) - 1): |
| 38 | + if self.arr[j] > self.arr[j + 1]: |
| 39 | + temp = self.arr[j] |
| 40 | + self.arr[j] = self.arr[j + 1] |
| 41 | + self.arr[j + 1] = temp |
| 42 | + return self.arr * multiplier |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + @codeflash_trace |
| 46 | + def sort_static(arr): |
| 47 | + for i in range(len(arr)): |
| 48 | + for j in range(len(arr) - 1): |
| 49 | + if arr[j] > arr[j + 1]: |
| 50 | + temp = arr[j] |
| 51 | + arr[j] = arr[j + 1] |
| 52 | + arr[j + 1] = temp |
| 53 | + return arr |
| 54 | + |
| 55 | + @classmethod |
| 56 | + @codeflash_trace |
| 57 | + def sort_class(cls, arr): |
| 58 | + for i in range(len(arr)): |
| 59 | + for j in range(len(arr) - 1): |
| 60 | + if arr[j] > arr[j + 1]: |
| 61 | + temp = arr[j] |
| 62 | + arr[j] = arr[j + 1] |
| 63 | + arr[j + 1] = temp |
| 64 | + return arr |
0 commit comments