diff --git a/libs/algorithms.py b/libs/algorithms.py index c82089a..e7461ad 100644 --- a/libs/algorithms.py +++ b/libs/algorithms.py @@ -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 \ No newline at end of file diff --git a/numberSort.py b/numberSort.py index d927488..adf60bb 100644 --- a/numberSort.py +++ b/numberSort.py @@ -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 @@ -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") @@ -49,11 +50,12 @@ ## 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") @@ -61,11 +63,26 @@ ## 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") \ No newline at end of file diff --git a/testsort.py b/testsort.py new file mode 100644 index 0000000..ff03d34 --- /dev/null +++ b/testsort.py @@ -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)) \ No newline at end of file