From: Skullheadx <704277@pdsb.net> Date: Tue, 24 Jan 2023 23:28:58 +0000 (-0500) Subject: Selection Sort added X-Git-Url: http://git.skullheadx.com/nixos/life/static/gitweb.js?a=commitdiff_plain;h=57adb1c87bbf88eefbd69d6e44c3cbb8b16e3f3b;p=Sorting.git Selection Sort added --- diff --git a/sorting_algorithms/selection_sort.py b/sorting_algorithms/selection_sort.py index e69de29..cff5ee3 100644 --- a/sorting_algorithms/selection_sort.py +++ b/sorting_algorithms/selection_sort.py @@ -0,0 +1,12 @@ +# Selection Sort (O(n^2)) +from utils import swap + + +def selection_sort(array): + for index1 in range(len(array) - 1, -1, -1): # loop through from end + swap_index = 0 # largest element to swap with + for index2 in range(index1 + 1): # loop through the first index1 elements + if array[index2] > array[swap_index]: # find the max element to swap with + swap_index = index2 # set the swap index + swap(array, index1, swap_index) # use swap the largest and the index1 (from end) + return array # return sorted array