diff options
author | internetlandlord <f.fredhenry@gmail.com> | 2022-11-09 21:10:29 +0000 |
---|---|---|
committer | internetlandlord <f.fredhenry@gmail.com> | 2022-11-09 21:10:29 +0000 |
commit | df5e8d731a5c36b509d537ed097de8d72c07a0c9 (patch) | |
tree | 59ad81220930c2afa6be208285fb932206c68c87 | |
parent | de9e7ac398ac4d7ea59422bdd9d9a22183f65a71 (diff) |
Added reverese heap and quicksort scripts
-rwxr-xr-x | reverse-heap-sort.py | 64 | ||||
-rwxr-xr-x | reverse-quick-sort.py | 44 |
2 files changed, 108 insertions, 0 deletions
diff --git a/reverse-heap-sort.py b/reverse-heap-sort.py new file mode 100755 index 0000000..ba6e8f6 --- /dev/null +++ b/reverse-heap-sort.py @@ -0,0 +1,64 @@ +#!/bin/python3 + +# ---------- Heap Sort Algorithm +# + + +# ---------- Imported modules +import inputoutput as io +import csv +import sys + + +# ---------- Main +target = sys.argv[1] + +# ingress of data +numdata = io.ingressCSV(target) + + +# Heap function (set up for algorithm) +def heapMake(data, node, root): + + # create root and define left and right tree branches + maxi = root + left = 2 * root + 1 + rite = 2 * root + 2 + + # check for existence of left child and if so, compare to root + if left < node and data[maxi] > data[left]:#FIXME: swapped second comparators to < + maxi = left + + # check for existence of right child and if so, compare to root + if rite < node and data[maxi] > data[rite]:#FIXME: swapped second compatarors to < + maxi = rite + + # if needed change out root value using a swap assignment + if maxi != root: + data[root], data[maxi] = data[maxi], data[root] + + # recursively make root into a heap + heapMake(data, node, maxi) + + +# Algorithm +def heapSort(atad): + span = len(atad) + + # buildup heap + for i in range(span // 2 - 1, -1, -1): + heapMake(atad, span, i) + + # extract heap elements single-file + for i in range(span - 1, 0, -1): + # swap assignment + atad[i], atad[0] = atad[0], atad[i] + heapMake(atad, i, 0) + +# using sorting algorithm +heapSort(numdata) + +# finishing up, appending "-heapsorted" to sorted CSV +target = target[:-4]+"-reverse-heap"+target[-4:] +print("Sorting done! Writing to file") +io.egressCSV(numdata,target) diff --git a/reverse-quick-sort.py b/reverse-quick-sort.py new file mode 100755 index 0000000..e3d48b2 --- /dev/null +++ b/reverse-quick-sort.py @@ -0,0 +1,44 @@ +#!/bin/python3 + +# ---------- Quick Sort +# Python implementation of the quick sort algorithm +# performed against a randomly-generated list of numbers +# Uses return arguments in the recursively called quickSort() +# function + + +# ---------- Imported modules +import inputoutput as io +import csv +import sys + + +# ---------- Main +target = sys.argv[1] + +# ingress of data +numdata = io.ingressCSV(target) + +# quick sort algorithm, defined as a function to enable recursive calling +def quickSort(data): + if(len(data)>1): + pivot = int(len(data)/2) + vcomp = data[pivot] + + leftmost = [i for i in data if i > vcomp]#FIXME: swapping comparator from < to > + middie = [i for i in data if i == vcomp] + ritemost = [i for i in data if i < vcomp]#FIXME: swapping comparator from > to < + + # recursively calling the quickSort function + recursor = quickSort(leftmost) + middie + quickSort(ritemost) + return recursor + else: + return data + +# running quicksort against the ingressed data +numdata = quickSort(numdata) + +# finishing up and appending "-quicksorted" to CSV +target = target[:-4]+"-reverse-quick"+target[-4:] +print("Sort complete, writing to file!") +io.egressCSV(numdata,target) |