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
74 changes: 66 additions & 8 deletions libs/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
##
# Module to store various algorithms
#
# Author: XCC
# Date: 10/25/2020
# Author: Luke Sequeira
# Date: November, 2020
#


def bubbleSort(list):
return list
def bubbleSort(a):
lst = a.copy()
lnt = len(lst)
for i in range(lnt):
for j in range(lnt - 1):
if(lst[j] > lst[j+1]):
lst[j], lst[j+1] = lst[j+1], lst[j] #switch j and j + 1
return lst


def insertionSort(list):
return list
def insertionSort(a):
lst = a.copy()
lnt = len(lst)
for i in range(lnt-1): #loop from 2nd to last element (since list of len 1 is trivially sorted)
key = lst[i + 1]
j = i
while(j >= 0 and key < lst[j]):
lst[j + 1] = lst[j]
j -= 1
lst[j + 1] = key
return lst


def selectionSort(list):
return list
def selectionSort(a):
lst = a.copy()
lnt = len(lst) - 1
for i in range(lnt):
key = i
for j in range(i + 1, lnt + 1):
if(lst[j] < lst[key]):
key = j
lst[key], lst[i] = lst[i], lst[key]
return lst

def mergeSort(a):
if(len(a) > 1): #non trivially-sorted list
mdle = len(a)//2 #find the mid-point of the list
L = a[:mdle] #left half of array
R = a[mdle:] #right half of array

#Call merge sort on the 2 halfs
mergeSort(L)
mergeSort(R)

#merge the 2 sorted halfs
i = 0
j = 0
k = 0
while(i < len(L) and j < len(R)): #this loop runs len(a) times
if(L[i] < R[j]):
a[k] = L[i]
i += 1
k+=1
else:
a[k] = R[j]
j += 1
k+=1
# Checking if any element was left
while i < len(L):
a[k] = L[i]
i += 1
k += 1

while j < len(R):
a[k] = R[j]
j += 1
k += 1
return a
37 changes: 27 additions & 10 deletions numberSort.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##
# Program to accept and sort numbers in a list, aswell as measure how long it took
#
# Author: XCC
# Author: Luke Sequeira
# Date: 10/25/2020

from libs import algorithms
Expand Down Expand Up @@ -36,11 +36,12 @@
## Bubble sort ##

# Output sorted
print("Sorted with bubble sort" + str(algorithms.bubbleSort(numList)))
foo = numList
print("Sorted with bubble sort" + str(algorithms.bubbleSort(foo)))

# Time the sorting with timeit.timeit
time = timeit.timeit('algorithms.bubbleSort(numList)',
'from __main__ import algorithms, numList')
time = timeit.timeit('algorithms.bubbleSort(foo)',
'from __main__ import algorithms, foo')

# Output the time it took in micro seconds
print("Time: " + str(time) + " micro sec")
Expand All @@ -49,23 +50,39 @@
## Insertion sort ##

# Output sorted
print("Sorted with insertion sort" + str(algorithms.insertionSort(numList)))
foo = numList
print("Sorted with insertion sort" + str(algorithms.insertionSort(foo)))

# Time the sorting with timeit.timeit
time = timeit.timeit('algorithms.insertionSort(numList)',
'from __main__ import algorithms, numList')
time = timeit.timeit('algorithms.insertionSort(foo)',
'from __main__ import algorithms, foo')

# Output the time it took in micro seconds
print("Time: " + str(time) + " micro sec")

## Selection sort ##

# Output sorted
print("Sorted with selection sort" + str(algorithms.selectionSort(numList)))
foo = numList
print("Sorted with selection sort" + str(algorithms.selectionSort(foo)))

# Time the sorting with timeit.timeit
time = timeit.timeit('algorithms.selectionSort(numList)',
'from __main__ import algorithms, numList')
time = timeit.timeit('algorithms.selectionSort(foo)',
'from __main__ import algorithms, foo')

# Output the time it took in micro seconds
print("Time: " + str(time) + " micro sec")


## Merge sort ##

# Output sorted
foo = numList
print("Sorted with merge sort" + str(algorithms.mergeSort(foo.copy())))

# Time the sorting with timeit.timeit
time = timeit.timeit('algorithms.selectionSort(foo)',
'from __main__ import algorithms, foo')

# Output the time it took in micro seconds
print("Time: " + str(time) + " micro sec")
10 changes: 10 additions & 0 deletions testsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from libs import algorithms as a
b = [3, 1, 2]
print([3, 1, 2])
print(a.bubbleSort([3, 1, 2]))
print(a.insertionSort([3, 1, 2]))
print(a.selectionSort([3, 1, 2]))

print(a.bubbleSort(b))
print(a.insertionSort(b))
print(a.selectionSort(b))