From 57adb1c87bbf88eefbd69d6e44c3cbb8b16e3f3b Mon Sep 17 00:00:00 2001 From: Skullheadx <704277@pdsb.net> Date: Tue, 24 Jan 2023 18:28:58 -0500 Subject: [PATCH] Selection Sort added --- sorting_algorithms/selection_sort.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 -- 2.54.0