]> Skullheadx's Git Forge - Sorting.git/commitdiff
fix bubble sort
authorSkullheadx <704277@pdsb.net>
Wed, 25 Jan 2023 00:05:04 +0000 (19:05 -0500)
committerSkullheadx <704277@pdsb.net>
Wed, 25 Jan 2023 00:05:04 +0000 (19:05 -0500)
sorting_algorithms/bubble_sort.py

index 669cebad53075c3c53e13ab09d955204c2d33a18..e387e2faa2d57c6dfab5ed3aafc5a8be5078bfcb 100644 (file)
@@ -3,9 +3,12 @@ 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)
+    for top in range(len(array) - 1, 0, -1):  # loop through each element from end
+        is_sorted = True  # to end early if already sorted
+        for index in range(top):  # loop through each of elements until reach top
+            if array[index] > array[index + 1]:  # if greater, swap
+                swap(array, index, index + 1)  # swap with next
+                is_sorted = False  # reset to not sorted
+        if is_sorted:  # break out if already sorted
+            break
     return array  # return sorted array
-