summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinternetlandlord <f.fredhenry@gmail.com>2022-10-21 17:10:18 +0000
committerinternetlandlord <f.fredhenry@gmail.com>2022-10-21 17:10:18 +0000
commit0bb0d85ea047871749005223b088b42196da5d5e (patch)
tree9066d62c8e29adcd10ef7bf3b8e2f0f1956d1911
parente56f9839a344f743889589b802932c9e0d1dd1b2 (diff)
Added merge sort algorithm file
-rwxr-xr-xmerge-sort.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/merge-sort.py b/merge-sort.py
new file mode 100755
index 0000000..683af05
--- /dev/null
+++ b/merge-sort.py
@@ -0,0 +1,61 @@
+#!/bin/python3
+
+# ---------- Merge Sort Algorithm
+
+
+
+# ---------- 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]:
+ 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):
+ 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 egress of data
+print("Sorting done, writing to file!")
+io.egressCSV(numdata,target)