From: Skullheadx <704277@pdsb.net> Date: Tue, 24 Jan 2023 23:32:02 +0000 (-0500) Subject: Add Bubble Sort X-Git-Url: http://git.skullheadx.com/nixos/static/projects.html?a=commitdiff_plain;h=467134dee2713f6fad30a71b611c71cac017985e;p=Sorting.git Add Bubble Sort --- diff --git a/sorting_algorithms/bubble_sort.py b/sorting_algorithms/bubble_sort.py index e69de29..669ceba 100644 --- a/sorting_algorithms/bubble_sort.py +++ b/sorting_algorithms/bubble_sort.py @@ -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 +