From 4430608135f3b5f1e4fba94522405fc79b67b194 Mon Sep 17 00:00:00 2001 From: Alex-Usmanov <40719830+Alex-Usmanov@users.noreply.github.com> Date: Fri, 19 Oct 2018 19:56:14 +0500 Subject: [PATCH] Implemented selection sort algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit O (n²) --- selectionSort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 selectionSort.py diff --git a/selectionSort.py b/selectionSort.py new file mode 100644 index 0000000..02ed7da --- /dev/null +++ b/selectionSort.py @@ -0,0 +1,27 @@ +def findLowest(list_: list): + """ + Finds the lowest number from the list and returns it + """ + lowest = list_[0] + for item in list_: + if item < lowest: + lowest = item + + return lowest + + +def selectionSort(list_: list): + """ + Sorts a list using selection sort algorithm + """ + array = list_ + result = [] + + while not len(array) == 0: + lowest = findLowest(array) + lowestIndex = array.index(lowest) + + result.append(lowest) + array.pop(lowestIndex) + + return result