summaryrefslogtreecommitdiff
path: root/reverse-merge-sort.py
blob: 54b6f463702f6bb6c6a7542e53ee2f5f1c55b551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)