From a6b5a9e4698fd9c4ee7232047c1df399bf11b23c Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Mon, 21 Jul 2025 20:40:04 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`s?= =?UTF-8?q?orter`=20by=20194,594%=20REFINEMENT=20Here=20is=20an=20optimize?= =?UTF-8?q?d=20version=20of=20your=20program.=20The=20provided=20implement?= =?UTF-8?q?ation=20uses=20an=20inefficient=20bubble=20sort=20(O(n=C2=B2)).?= =?UTF-8?q?=20Python's=20built-in=20`sort`=20method=20is=20much=20faster?= =?UTF-8?q?=20(Timsort,=20O(n=20log=20n))=20and=20sorts=20in=20place.=20Al?= =?UTF-8?q?l=20outputs=20are=20preserved=20and=20the=20return=20value=20re?= =?UTF-8?q?mains=20the=20same.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will run dramatically faster and is also less memory intensive. --- code_to_optimize/bubble_sort.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/code_to_optimize/bubble_sort.py b/code_to_optimize/bubble_sort.py index 9e97f63a0..7dc644cde 100644 --- a/code_to_optimize/bubble_sort.py +++ b/code_to_optimize/bubble_sort.py @@ -1,10 +1,5 @@ def sorter(arr): print("codeflash stdout: Sorting list") - for i in range(len(arr)): - for j in range(len(arr) - 1): - if arr[j] > arr[j + 1]: - temp = arr[j] - arr[j] = arr[j + 1] - arr[j + 1] = temp + arr.sort() print(f"result: {arr}") return arr