diff options
author | internetlandlord <f.fredhenry@gmail.com> | 2022-10-26 16:50:35 +0000 |
---|---|---|
committer | internetlandlord <f.fredhenry@gmail.com> | 2022-10-26 16:50:35 +0000 |
commit | ff55f26f024ff6eaa72b25239906768872bb94ff (patch) | |
tree | 8a462252cf7de0c35626404bcc81b14420c963fa | |
parent | c63df88afb86cab2de9a22d22030c8687cb25f83 (diff) |
Added a heap-sort algorithm, as well as updating README.md accordingly
-rw-r--r-- | README.md | 4 | ||||
-rwxr-xr-x | heap-sort.py | 63 |
2 files changed, 66 insertions, 1 deletions
@@ -12,6 +12,7 @@ The functional file contains I/O functions for ingressing and egressing data. * Quick Sort * Bubble Sort * Merge Sort +* Heap Sort More to come! @@ -19,6 +20,7 @@ More to come! ## Ideas - Reverse order sorting algorithms (to kick it up a notch) - time study (how long does each take) + complexity log +- seperate appends depending on each algorithm used ## Other funcions @@ -33,5 +35,5 @@ exporting data to csv * Used to create randomly-generated CSVs for testing algorithm performance #### tripler.py -* Serves as a proof-of-concept for file manipulation +* Serves as a proof-of-concept for file content manipulation diff --git a/heap-sort.py b/heap-sort.py new file mode 100755 index 0000000..c8a45eb --- /dev/null +++ b/heap-sort.py @@ -0,0 +1,63 @@ +#!/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]: + maxi = left + + # check for existence of right child and if so, compare to root + if rite < node and data[maxi] < data[rite]: + 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 +print("Sorting done! Writing to file") +io.egressCSV(numdata,target) |