diff options
author | internetlandlord <f.fredhenry@gmail.com> | 2022-11-03 20:30:31 +0000 |
---|---|---|
committer | internetlandlord <f.fredhenry@gmail.com> | 2022-11-03 20:30:31 +0000 |
commit | 4abdc0d451d254cdfcff42eec1f3998afbf8fa49 (patch) | |
tree | 54c8b988e8cb09fb114be74c3557aefe28a63a5e | |
parent | cb887cc85943b96172a935f4dac3828c1e087a1d (diff) |
Updated appended filenames for reverse quick and insertion sorts. Created reverse merge sort algorithm script.
-rwxr-xr-x | reverse-bubble-sort.py | 2 | ||||
-rwxr-xr-x | reverse-insertion-sort.py | 2 | ||||
-rwxr-xr-x | reverse-merge-sort.py | 62 |
3 files changed, 64 insertions, 2 deletions
diff --git a/reverse-bubble-sort.py b/reverse-bubble-sort.py index a13f4cb..c166830 100755 --- a/reverse-bubble-sort.py +++ b/reverse-bubble-sort.py @@ -24,6 +24,6 @@ for i in range(len(numdata)): numdata[j], numdata[j+1] = numdata[j+1], numdata[j] # finishing up, appending "-bubblesorted" to CSV -target = target[:-4]+"-REV-bubblesorted"+target[-4:] +target = target[:-4]+"-reverse-bubble"+target[-4:] print("Sorting complete! Writing to file.") io.egressCSV(numdata,target) diff --git a/reverse-insertion-sort.py b/reverse-insertion-sort.py index 3bdb2dd..47c318b 100755 --- a/reverse-insertion-sort.py +++ b/reverse-insertion-sort.py @@ -33,6 +33,6 @@ def insertionSort(data): insertionSort(numdata) # wrapping up and appending "-insertionsorted" to CSV -target = target[:-4]+"-REV-insertionsorted"+target[-4:] +target = target[:-4]+"-reverse-insertion"+target[-4:] print("Sorting done! Writing to file.") io.egressCSV(numdata,target) diff --git a/reverse-merge-sort.py b/reverse-merge-sort.py new file mode 100755 index 0000000..54b6f46 --- /dev/null +++ b/reverse-merge-sort.py @@ -0,0 +1,62 @@ +#!/bin/python3 + +# ---------- REVERSE Merge Sort Algorithm +# Merge sort... but in reverse! + + +# ---------- Imported modules +import inputoutput as io +import csv +import sys + + +# ---------- Main +target = sys.argv[1] + +# ingress of data +numdata = io.ingressCSV(target) + +# merge sorting +def mergeSort(data): + if len(data) > 1: + # perition out list + middie = len(data) // 2 + leftmost = data[:middie] + ritemost = data[middie:] + + # sorting left and right recursively + mergeSort(leftmost) + mergeSort(ritemost) + + # iterator initializations + i = j = k = 0 + + # populating temporary arrays with data + while i < len(leftmost) and j < len(ritemost): + if leftmost[i] >= ritemost[j]: #FIXME: flipped around to reverse + data[k] = leftmost[i] + i += 1 + else: + data[k] = ritemost[j] + j += 1 + + k += 1 + + # cleanup for any elements left behind + while i < len(leftmost): #FIXME: do these need flipping? + data[k] = leftmost[i] + i += 1 + k += 1 + + while j < len(ritemost): + data[k] = ritemost[j] + j += 1 + k += 1 + +# calling the function +mergeSort(numdata) + +# finishing up and appending "-mergesorted" to CSV +target = target[:-4]+"-reverse-merge"+target[-4:] +print("Sorting done, writing to file!") +io.egressCSV(numdata,target) |