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+
34def bubble_sort (collection : list [Any ]) -> list [Any ]:
45 """Non-optimised O(n^2) implementation of bubble sort algorithm (iterative)"""
56 n = len (collection )
67 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 ]
8+ for j in range (n - 1 - i ): # number of pairs
9+ if collection [j ] > collection [j + 1 ]:
10+ collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
1011 return collection
1112
13+
1214def bubble_sort_iterative (collection : list [Any ]) -> list [Any ]:
1315 """Pure implementation of bubble sort algorithm in Python
1416
@@ -53,7 +55,7 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
5355 length = len (collection )
5456 for i in reversed (range (length )):
5557 swapped = False
56- for j in range (i ): # number of pairs
58+ for j in range (i ): # number of pairs
5759 if collection [j ] > collection [j + 1 ]:
5860 swapped = True
5961 collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
You can’t perform that action at this time.
0 commit comments