From a84b88d87aaa5cc9fd058ae5d865984579824a30 Mon Sep 17 00:00:00 2001 From: ayaandada <73909370+ayaandada@users.noreply.github.com> Date: Tue, 3 Nov 2020 17:22:24 -0500 Subject: [PATCH] added bubble, insertion, and selection sort --- libs/algorithms.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/libs/algorithms.py b/libs/algorithms.py index c82089a..61aafff 100644 --- a/libs/algorithms.py +++ b/libs/algorithms.py @@ -2,17 +2,39 @@ # Module to store various algorithms # # Author: XCC -# Date: 10/25/2020 -# +# Date: 11/03/2020 +# Edited by: Ayaan D. -def bubbleSort(list): - return list +def bubbleSort(ls): + lengthOfList = len(numList) + for i in range(lengthOfList): + for j in range(0, lengthOfList - i - 1): + if numList[j] > numList[j+1]: + numList[j], numList[j+1] = numList[j+1], numList[j] + return ls -def insertionSort(list): - return list +def insertionSort(ls): + lengthOfList = len(numList) + for i in range(1, lengthOfList): + position = numList[i] + j = i-1 + while j >= 0 and position < numList[j]: + numList[j+1] = numList[j] + j -= 1 + numList[j+1] = position + + return ls -def selectionSort(list): - return list +def selectionSort(ls): + lengthOfList = len(numList) + for i in range(lengthOfList): + indexOfMinimum = i + for j in range(i+1, lengthOfList): + if numList[indexOfMinimum] > numList[j]: + indexOfMinimum = j + numList[i], numList[indexOfMinimum] = numList[indexOfMinimum], numList[i] + + return ls