Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libs/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 22 additions & 19 deletions numberSort.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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")