From df5e8d731a5c36b509d537ed097de8d72c07a0c9 Mon Sep 17 00:00:00 2001 From: internetlandlord Date: Wed, 9 Nov 2022 21:10:29 +0000 Subject: Added reverese heap and quicksort scripts --- reverse-quick-sort.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 reverse-quick-sort.py (limited to 'reverse-quick-sort.py') 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) -- cgit v1.2.3