diff --git a/libs/algorithms.py b/libs/algorithms.py index c82089a..4757127 100644 --- a/libs/algorithms.py +++ b/libs/algorithms.py @@ -7,12 +7,32 @@ def bubbleSort(list): + list = list.copy() + for i in range(len(list)): + for i in range(len(list) - 1): + if list[i] > list[i + 1]: + list[i], list[i + 1] = list[i+1], list[i] return list def insertionSort(list): + list = list.copy() + for i in range(1, len(list)): + j = 0 + while list[i] > list[j]: + j += 1 + num = list[i] + list.pop(i) + list.insert(j, num) return list def selectionSort(list): + list = list.copy() + for i in range(len(list)): + num = i + for j in range(i + 1, len(list)): + if list[num] > list[j]: + num = j + list[i], list[num] = list[num], list[i] return list diff --git a/numberSort.py b/numberSort.py index d927488..0dd713f 100644 --- a/numberSort.py +++ b/numberSort.py @@ -1,5 +1,5 @@ ## -# Program to accept and sort numbers in a list, aswell as measure how long it took +# Program to accept and sort numbers in a list, as well as measure how long it took # # Author: XCC # Date: 10/25/2020 @@ -13,12 +13,13 @@ done = False # Loop until user is satisfied -while(not done): +while not done: numInput = input( - "Enter a number to add to the list, if you wish to stop adding numbers enter 'q': \n") + "Enter a number to add to the list, if you wish to stop adding numbers enter 'q': \n" + ) # Quit out of loop when user says to - if numInput == 'q': + if numInput.lower() == 'q': done = True break @@ -28,44 +29,46 @@ except: print("Please enter a single number") -### Output the list sorted with various algoritms ### +### Output the list sorted with specified algoritms ### # To compare, output the unsorted list -print("Unsorted list:" + str(numList)) +print(f'Unsorted list: {numList}') ## Bubble sort ## # Output sorted -print("Sorted with bubble sort" + str(algorithms.bubbleSort(numList))) +print(f"Sorted with bubble sort {algorithms.bubbleSort(numList)}") # Time the sorting with timeit.timeit -time = timeit.timeit('algorithms.bubbleSort(numList)', - 'from __main__ import algorithms, numList') +time = timeit.timeit( + 'algorithms.bubbleSort(numList)', 'from __main__ import algorithms, numList' +) # Output the time it took in micro seconds -print("Time: " + str(time) + " micro sec") +print(f"Time: {time} micro sec") ## Insertion sort ## - # Output sorted -print("Sorted with insertion sort" + str(algorithms.insertionSort(numList))) +print(f"Sorted with insertion sort {algorithms.insertionSort(numList)}") # Time the sorting with timeit.timeit -time = timeit.timeit('algorithms.insertionSort(numList)', - 'from __main__ import algorithms, numList') +time = timeit.timeit( + 'algorithms.insertionSort(numList)', 'from __main__ import algorithms, numList' +) # Output the time it took in micro seconds -print("Time: " + str(time) + " micro sec") +print(f"Time: {time} micro sec") ## Selection sort ## # Output sorted -print("Sorted with selection sort" + str(algorithms.selectionSort(numList))) +print(f"Sorted with selection sort {algorithms.selectionSort(numList)}") # Time the sorting with timeit.timeit -time = timeit.timeit('algorithms.selectionSort(numList)', - 'from __main__ import algorithms, numList') +time = timeit.timeit( + 'algorithms.selectionSort(numList)', 'from __main__ import algorithms, numList' +) # Output the time it took in micro seconds -print("Time: " + str(time) + " micro sec") +print(f"Time: {time} micro sec")