summaryrefslogtreecommitdiff
path: root/quick-sort.py
blob: 1f807dd65dbcfd4157d160d5d5d10c0b7b7c1c61 (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
#!/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]
        middie = [i for i in data if i == vcomp]
        ritemost = [i for i in data if i > vcomp]

        # 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
print("Sort complete, writing to file!")
io.egressCSV(numdata,target)