File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from typing import Any
22
3+ def bubble_sort (collection : list [Any ]) -> list [Any ]:
4+ """Non-optimised O(n^2) implementation of bubble sort algorithm (iterative)"""
5+ n = len (collection )
6+ for i in range (n ):
7+ for j in range (n - 1 - i ): # number of pairs
8+ if collection [j ] > collection [j + 1 ]:
9+ collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
10+ return collection
311
412def bubble_sort_iterative (collection : list [Any ]) -> list [Any ]:
513 """Pure implementation of bubble sort algorithm in Python
@@ -45,7 +53,7 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
4553 length = len (collection )
4654 for i in reversed (range (length )):
4755 swapped = False
48- for j in range (i ):
56+ for j in range (i ): # number of pairs
4957 if collection [j ] > collection [j + 1 ]:
5058 swapped = True
5159 collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
You can’t perform that action at this time.
0 commit comments