]> Skullheadx's Git Forge - Sorting.git/commitdiff
Add Bubble Sort
authorSkullheadx <704277@pdsb.net>
Tue, 24 Jan 2023 23:32:02 +0000 (18:32 -0500)
committerSkullheadx <704277@pdsb.net>
Tue, 24 Jan 2023 23:32:02 +0000 (18:32 -0500)
sorting_algorithms/bubble_sort.py

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..669cebad53075c3c53e13ab09d955204c2d33a18 100644 (file)
@@ -0,0 +1,11 @@
+# Bubble Sort (O(n^2))
+from utils import swap
+
+
+def bubble_sort(array):
+    for index1 in range(len(array)):  # loop through each element
+        for index2 in range(index1, len(array)):  # loop through each of the next elements
+            if array[index2] < array[index1]:  # if lesser, then swap
+                swap(array, index1, index2)
+    return array  # return sorted array
+