summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinternetlandlord <f.fredhenry@gmail.com>2022-11-02 17:53:23 -0500
committerinternetlandlord <f.fredhenry@gmail.com>2022-11-02 17:53:23 -0500
commitcb887cc85943b96172a935f4dac3828c1e087a1d (patch)
treed68b1de285a64c038e5eb6e36b16ae7f06b67df8
parent6c7a50854c73575d53e93a584d6d9848493adda3 (diff)
reverse bubble sort and insertion sort now working
-rwxr-xr-xreverse-bubble-sort.py7
-rwxr-xr-xreverse-insertion-sort.py38
2 files changed, 41 insertions, 4 deletions
diff --git a/reverse-bubble-sort.py b/reverse-bubble-sort.py
index 109ab52..a13f4cb 100755
--- a/reverse-bubble-sort.py
+++ b/reverse-bubble-sort.py
@@ -1,10 +1,9 @@
#!/bin/python3
# ---------- REVERSE Bubble Sort Algorithm
-# Python implementation of bubble sort algorithm
-# uses the csv, and inputoutput modules to import
-# CSVs and sort the numbers in a list before writing
-# to a CSV file with an appended -sorted title.
+# Bubble sort in reverse (greatest --> least)
+# Very simple implementation due to straightforward
+# layout of algorithm
# ---------- Imported modules
import inputoutput as io
diff --git a/reverse-insertion-sort.py b/reverse-insertion-sort.py
new file mode 100755
index 0000000..3bdb2dd
--- /dev/null
+++ b/reverse-insertion-sort.py
@@ -0,0 +1,38 @@
+#!/bin/python3
+
+# --------- REVERSE Insertion Sort Algorithm
+# Implementation of algorithm in python
+
+# --------- Imported modules
+import inputoutput as io
+import csv
+import sys
+
+
+# ingressing data through cli arguments and imported modules
+target = sys.argv[1]
+numdata = io.ingressCSV(target)
+
+# insertion sort algorithm, as function
+def insertionSort(data):
+ for i in range(1, len(data)):
+ # setting the key to the current entry in row
+ key = data[i]
+ #
+ j = i - 1
+
+ # left comparison FIXME: flipped key < data[j]
+ while j >= 0 and key > data[j]:
+ data[j + 1] = data[j]
+ j = j - 1
+
+ # move key after element smaller than it
+ data[j + 1] = key
+
+# sorting call
+insertionSort(numdata)
+
+# wrapping up and appending "-insertionsorted" to CSV
+target = target[:-4]+"-REV-insertionsorted"+target[-4:]
+print("Sorting done! Writing to file.")
+io.egressCSV(numdata,target)