Major refactor of files
This commit is contained in:
parent
f3dabfdb65
commit
0fbc5b8bea
4
Makefile
4
Makefile
@ -5,7 +5,9 @@ main: compile link
|
||||
|
||||
compile:
|
||||
g++ $(INC) $(STD) -c -o build/main.o src/main.cpp
|
||||
g++ $(INC) $(STD) -c -o build/sorts.o src/sorts.cpp
|
||||
g++ $(INC) $(STD) -c -o build/sort_controller.o src/sort_controller.cpp
|
||||
g++ $(INC) $(STD) -c -o build/basic_sorts.o src/basic_sorts.cpp
|
||||
g++ $(INC) $(STD) -c -o build/trees.o src/trees.cpp
|
||||
|
||||
link:
|
||||
g++ -o bin/main.out build/*.o
|
||||
|
26
include/basic_sorts.hpp
Normal file
26
include/basic_sorts.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef BASIC_SORTS_HPP
|
||||
#define BASIC_SORTS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Different basic sorting algorithm implementations
|
||||
namespace basic_sorts
|
||||
{
|
||||
// Performs Insertion Sort on given word list (vector of strings)
|
||||
void InsertionSort(std::vector<std::string> *newWordList);
|
||||
|
||||
// Performs Merge Sort on given word list (vector of strings)
|
||||
void MergeSort(std::vector<std::string> *newWordList);
|
||||
void _MergeSort(std::vector<std::string> *newWordList, int p, int r);
|
||||
void _Merge(std::vector<std::string> *newWordList, int p, int q, int r);
|
||||
|
||||
// Performs Heap Sort on given word list (vector of strings)
|
||||
void HeapSort(std::vector<std::string> *newWordList);
|
||||
void _HeapSort(std::vector<std::string> *newWordList);
|
||||
void _Heapify(std::vector<std::string> *newWordList, int i, int heapSize);
|
||||
int _HEAPSORT_LEFT(int i);
|
||||
int _HEAPSORT_RIGHT(int i);
|
||||
}
|
||||
|
||||
#endif
|
34
include/sort_controller.hpp
Normal file
34
include/sort_controller.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef SORT_CONTROLLER_HPP
|
||||
#define SORT_CONTROLLER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum SortType {INSERTION = 0, MERGE, HEAP};
|
||||
|
||||
class SortController
|
||||
{
|
||||
public:
|
||||
SortController();
|
||||
void CheckArguments(int argc, char* arguments[]);
|
||||
void ReadWordFile(void);
|
||||
void RunBenchmarks(void);
|
||||
protected:
|
||||
;
|
||||
private:
|
||||
std::string filename;
|
||||
SortType currentType;
|
||||
std::chrono::duration<double> sortTime;
|
||||
std::vector<std::string> newWordList;
|
||||
std::vector<std::string> originalWordList;
|
||||
int lineCount;
|
||||
bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven;
|
||||
void Benchmarking(void);
|
||||
void BenchmarkingAll(void);
|
||||
void OutputResult(void);
|
||||
void EchoSortTime(std::string outputFilename);
|
||||
void WriteOutputToFile(std::string outputFilename);
|
||||
};
|
||||
|
||||
#endif
|
@ -1,53 +0,0 @@
|
||||
#ifndef SORTS_H
|
||||
#define SORTS_H
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum SortType {INSERTION = 0, MERGE, HEAP};
|
||||
|
||||
class Sorter
|
||||
{
|
||||
private:
|
||||
std::string filename;
|
||||
int lineCount;
|
||||
std::vector<std::string> originalWordList;
|
||||
std::chrono::duration<double> sortTime;
|
||||
public:
|
||||
Sorter();
|
||||
std::vector<std::string> newWordList;
|
||||
bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven;
|
||||
SortType currentType;
|
||||
std::string GetFilename(void);
|
||||
void SetFilename(std::string newName);
|
||||
void SetWordList(void);
|
||||
void SortAll(void);
|
||||
void RunSorts(void);
|
||||
void __RunSorts__(void);
|
||||
void OutputResult(void);
|
||||
void PrintSortTime(std::string outputFilename);
|
||||
void PrintToFile(std::string outputFilename);
|
||||
};
|
||||
|
||||
void CheckArguments(int argc, char* arguments[], Sorter* sortObj);
|
||||
|
||||
namespace ImplementedSort
|
||||
{
|
||||
// Performs Insertion Sort on given word list (vector of strings)
|
||||
void InsertionSort(std::vector<std::string> *newWordList);
|
||||
|
||||
// Performs Merge Sort on given word list (vector of strings)
|
||||
void MergeSort(std::vector<std::string> *newWordList);
|
||||
void __MergeSort__(std::vector<std::string> *newWordList, int p, int r);
|
||||
void __Merge__(std::vector<std::string> *newWordList, int p, int q, int r);
|
||||
|
||||
// Performs Heap Sort on given word list (vector of strings)
|
||||
void HeapSort(std::vector<std::string> *newWordList);
|
||||
void __HeapSort__(std::vector<std::string> *newWordList);
|
||||
void __Heapify__(std::vector<std::string> *newWordList, int i, int heapSize);
|
||||
int __HEAPSORT_LEFT__(int i);
|
||||
int __HEAPSORT_RIGHT__(int i);
|
||||
}
|
||||
|
||||
#endif
|
61
include/trees.hpp
Normal file
61
include/trees.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
#ifndef TREES_HPP
|
||||
#define TREES_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Namespace for different implementations of trees
|
||||
namespace tree_implementation
|
||||
{
|
||||
// Base Tree class
|
||||
class Tree
|
||||
{
|
||||
public:
|
||||
Tree(void);
|
||||
void Insert(void);
|
||||
void Search(void);
|
||||
void InOrderTreeTraversal(void);
|
||||
protected:
|
||||
virtual void PrintParentKey(std::string key) = 0;
|
||||
virtual void PrintLeftChild(std::string key) = 0;
|
||||
virtual void PrintRightChild(std::string key) = 0;
|
||||
virtual void PrintPathToRoot(std::string key) = 0;
|
||||
private:
|
||||
;
|
||||
};
|
||||
|
||||
// Binary Search Tree operations
|
||||
// TODO: Implement BST
|
||||
class BinarySearchTree : public Tree
|
||||
{
|
||||
public:
|
||||
void PrintParentKey(std::string key);
|
||||
void PrintLeftChild(std::string key);
|
||||
void PrintRightChild(std::string key);
|
||||
void PrintPathToRoot(std::string key);
|
||||
protected:
|
||||
;
|
||||
private:
|
||||
;
|
||||
};
|
||||
|
||||
// Red-Black Tree operations
|
||||
// TODO: Implement Red-black tree
|
||||
class RedBlackTree : public Tree
|
||||
{
|
||||
public:
|
||||
void PrintParentKey(std::string key);
|
||||
void PrintLeftChild(std::string key);
|
||||
void PrintRightChild(std::string key);
|
||||
void PrintPathToRoot(std::string key);
|
||||
void PrintColor(std::string key);
|
||||
void PrintParentColor(std::string key);
|
||||
void PrintUncleColor(std::string key);
|
||||
protected:
|
||||
;
|
||||
private:
|
||||
;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
129
src/basic_sorts.cpp
Normal file
129
src/basic_sorts.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "basic_sorts.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace basic_sorts
|
||||
{
|
||||
// Reimplementation of Insertion Sort to be standalone
|
||||
void InsertionSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
int i;
|
||||
std::string key;
|
||||
for (int j = 1; j < newWordList->size(); j++)
|
||||
{
|
||||
key = newWordList->at(j);
|
||||
i = j - 1;
|
||||
while ((i >= 0) && (newWordList->at(i) > key))
|
||||
{
|
||||
newWordList->at(i + 1) = newWordList->at(i);
|
||||
i = i - 1;
|
||||
}
|
||||
newWordList->at(i + 1) = key;
|
||||
}
|
||||
}
|
||||
|
||||
// Reimplementation of Merge Sort to be standalone
|
||||
void MergeSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
_MergeSort(newWordList, 0, newWordList->size() - 1);
|
||||
return;
|
||||
}
|
||||
|
||||
void _MergeSort(std::vector<std::string> *newWordList, int p, int r)
|
||||
{
|
||||
if (p < r)
|
||||
{
|
||||
int q = (p + r) / 2;
|
||||
_MergeSort(newWordList, p, q);
|
||||
_MergeSort(newWordList, q+1, r);
|
||||
_Merge(newWordList, p, q, r);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void _Merge(std::vector<std::string> *newWordList, int p, int q, int r)
|
||||
{
|
||||
int n1 = q - p + 1;
|
||||
int n2 = r - q;
|
||||
const int leftTmpSize = n1 + 1;
|
||||
const int rightTmpSize = n2 + 1;
|
||||
std::string leftTmp[leftTmpSize];
|
||||
std::string rightTmp[rightTmpSize];
|
||||
for (int i = 0; i < n1; i++)
|
||||
leftTmp[i] = newWordList->at(p + i);
|
||||
for (int i = 0; i < n2; i++)
|
||||
rightTmp[i] = newWordList->at(q + i + 1);
|
||||
leftTmp[n1] = "ZZZZZ";
|
||||
rightTmp[n2] = "ZZZZZ";
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
for (int k = p; k <= r; k++)
|
||||
{
|
||||
if (leftTmp[i] <= rightTmp[j])
|
||||
{
|
||||
newWordList->at(k) = leftTmp[i];
|
||||
i++;
|
||||
} else {
|
||||
newWordList->at(k) = rightTmp[j];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Reimplementation of Heap Sort to be standalone
|
||||
void HeapSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
_HeapSort(newWordList);
|
||||
return;
|
||||
}
|
||||
|
||||
void _HeapSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
std::string tempStr;
|
||||
int heapSize;
|
||||
// Build Max Heap
|
||||
heapSize = newWordList->size();
|
||||
for (int i = ((newWordList->size() / 2) - 1); i >= 0; i--)
|
||||
_Heapify(newWordList, i, heapSize);
|
||||
// Heap Sort
|
||||
for (int i = (newWordList->size() - 1); i >= 0; i--)
|
||||
{
|
||||
tempStr = newWordList->at(i);
|
||||
newWordList->at(i) = newWordList->at(0);
|
||||
newWordList->at(0) = tempStr;
|
||||
heapSize = i;
|
||||
_Heapify(newWordList, 0, heapSize);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void _Heapify(std::vector<std::string> *newWordList, int i, int heapSize)
|
||||
{
|
||||
std::string tempStr;
|
||||
int largest = i;
|
||||
int leftIndex = _HEAPSORT_LEFT(i);
|
||||
int rightIndex = _HEAPSORT_RIGHT(i);
|
||||
if ((leftIndex < heapSize) && (newWordList->at(leftIndex) > newWordList->at(largest)))
|
||||
largest = leftIndex;
|
||||
if ((rightIndex < heapSize) && (newWordList->at(rightIndex) > newWordList->at(largest)))
|
||||
largest = rightIndex;
|
||||
if (largest != i)
|
||||
{
|
||||
tempStr = newWordList->at(i);
|
||||
newWordList->at(i) = newWordList->at(largest);
|
||||
newWordList->at(largest) = tempStr;
|
||||
_Heapify(newWordList, largest, heapSize);
|
||||
}
|
||||
}
|
||||
|
||||
int _HEAPSORT_LEFT(int i)
|
||||
{
|
||||
return ((2 * i) + 1);
|
||||
}
|
||||
|
||||
int _HEAPSORT_RIGHT(int i)
|
||||
{
|
||||
return ((2 * i) + 2);
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
#include "sorts.h"
|
||||
#include "sort_controller.hpp"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
Sorter newSort;
|
||||
CheckArguments(argc, argv, &newSort);
|
||||
newSort.RunSorts();
|
||||
SortController benchmark;
|
||||
benchmark.CheckArguments(argc, argv);
|
||||
benchmark.RunBenchmarks();
|
||||
return 0;
|
||||
}
|
||||
|
197
src/sort_controller.cpp
Normal file
197
src/sort_controller.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
#include "sort_controller.hpp"
|
||||
#include "basic_sorts.hpp"
|
||||
#include "trees.hpp"
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Initialization function for SortController class
|
||||
SortController::SortController()
|
||||
{
|
||||
lineCount = 0;
|
||||
currentType = INSERTION;
|
||||
defaultFile = 0;
|
||||
defaultOnly = 1;
|
||||
fileGiven = 0;
|
||||
allLists = 0;
|
||||
sortGiven = 0;
|
||||
}
|
||||
|
||||
// Sets word list found in file into vector
|
||||
void SortController::ReadWordFile(void)
|
||||
{
|
||||
std::string bufferStr;
|
||||
std::ifstream file(this->filename);
|
||||
if (!file.is_open())
|
||||
{
|
||||
std::cout << "Failed opening file: " << this->filename << '\n';
|
||||
exit(1);
|
||||
}
|
||||
while (getline(file, bufferStr))
|
||||
{
|
||||
originalWordList.push_back(bufferStr);
|
||||
lineCount++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Main function for calling all sort categories
|
||||
void SortController::RunBenchmarks(void)
|
||||
{
|
||||
if (defaultOnly || fileGiven)
|
||||
{
|
||||
ReadWordFile();
|
||||
Benchmarking();
|
||||
}
|
||||
if (allLists)
|
||||
BenchmarkingAll();
|
||||
return;
|
||||
}
|
||||
|
||||
// Sorts all default files if allLists is set
|
||||
void SortController::BenchmarkingAll(void)
|
||||
{
|
||||
int fileCount = 10;
|
||||
std::string newFilename;
|
||||
for (int i = 1; i <= fileCount; i++)
|
||||
{
|
||||
lineCount = 0;
|
||||
newFilename = "test/PERM/perm";
|
||||
newFilename += std::to_string(15*i);
|
||||
newFilename += "K.txt";
|
||||
filename = newFilename;
|
||||
ReadWordFile();
|
||||
Benchmarking();
|
||||
originalWordList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Function for starting sort functions
|
||||
void SortController::Benchmarking(void)
|
||||
{
|
||||
if (!sortGiven)
|
||||
currentType = INSERTION;
|
||||
if (currentType == INSERTION)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
basic_sorts::InsertionSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (!sortGiven)
|
||||
currentType = MERGE;
|
||||
if (currentType == MERGE)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
basic_sorts::MergeSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (!sortGiven)
|
||||
currentType = HEAP;
|
||||
if (currentType == HEAP)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
basic_sorts::HeapSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
}
|
||||
|
||||
// Main function for printing results
|
||||
void SortController::OutputResult(void)
|
||||
{
|
||||
switch(currentType)
|
||||
{
|
||||
case INSERTION:
|
||||
EchoSortTime("IS");
|
||||
WriteOutputToFile("IS");
|
||||
break;
|
||||
case MERGE:
|
||||
EchoSortTime("MS");
|
||||
WriteOutputToFile("MS");
|
||||
break;
|
||||
case HEAP:
|
||||
EchoSortTime("HS");
|
||||
WriteOutputToFile("HS");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Prints sort time to the screen
|
||||
void SortController::EchoSortTime(std::string outputFilename)
|
||||
{
|
||||
std::string sortSizeString = std::to_string(lineCount / 1000);
|
||||
std::cout << outputFilename << sortSizeString;
|
||||
std::cout << " took " << sortTime.count() << " s" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Prints result of sort operation to file
|
||||
void SortController::WriteOutputToFile(std::string outputFilename)
|
||||
{
|
||||
std::string sortSizeString = std::to_string(lineCount / 1000);
|
||||
std::string outputPath = "test/OUTPUT/";
|
||||
outputPath += outputFilename + sortSizeString + "K.txt";
|
||||
std::ofstream file(outputPath);
|
||||
if (!file.is_open())
|
||||
{
|
||||
std::cout << "Failed opening file\n";
|
||||
exit(1);
|
||||
}
|
||||
for (unsigned int i = 0; i < lineCount; i++)
|
||||
file << newWordList[i] << '\n';
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Checks for command line arguments
|
||||
void SortController::CheckArguments(int argc, char* arguments[])
|
||||
{
|
||||
std::string tempStr;
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
tempStr = arguments[i];
|
||||
if ((tempStr == "-a") || (tempStr == "--all"))
|
||||
{
|
||||
defaultOnly = 0;
|
||||
allLists = 1;
|
||||
}
|
||||
if ((tempStr == "-f") || (tempStr == "--filename"))
|
||||
{
|
||||
filename = arguments[i + 1];
|
||||
defaultOnly = 0;
|
||||
fileGiven = 1;
|
||||
}
|
||||
if ((tempStr == "-d") || (tempStr == "--default"))
|
||||
{
|
||||
filename = "test/PERM/perm15K.txt";
|
||||
defaultFile = 1;
|
||||
}
|
||||
if ((tempStr == "-s") || (tempStr == "--sort-type"))
|
||||
{
|
||||
sortGiven = 1;
|
||||
tempStr = arguments[i + 1];
|
||||
if (tempStr == "insertion")
|
||||
currentType = INSERTION;
|
||||
if (tempStr == "merge")
|
||||
currentType = MERGE;
|
||||
if (tempStr == "heap")
|
||||
currentType = HEAP;
|
||||
if (tempStr == "all")
|
||||
sortGiven = 0;
|
||||
}
|
||||
}
|
||||
if (defaultOnly)
|
||||
filename = "test/PERM/perm15K.txt";
|
||||
return;
|
||||
}
|
334
src/sorts.cpp
334
src/sorts.cpp
@ -1,334 +0,0 @@
|
||||
#include "sorts.h"
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Initialization function for Sorter class
|
||||
Sorter::Sorter()
|
||||
{
|
||||
lineCount = 0;
|
||||
currentType = INSERTION;
|
||||
defaultFile = 0;
|
||||
defaultOnly = 1;
|
||||
fileGiven = 0;
|
||||
allLists = 0;
|
||||
sortGiven = 0;
|
||||
}
|
||||
|
||||
// Sets the current filename
|
||||
void Sorter::SetFilename(std::string newName)
|
||||
{
|
||||
this->filename = newName;
|
||||
}
|
||||
|
||||
// Returns the current filename
|
||||
std::string Sorter::GetFilename(void)
|
||||
{
|
||||
return this->filename;
|
||||
}
|
||||
|
||||
// Sets word list found in file into vector
|
||||
void Sorter::SetWordList(void)
|
||||
{
|
||||
std::string bufferStr;
|
||||
std::ifstream file(this->filename);
|
||||
if (!file.is_open())
|
||||
{
|
||||
std::cout << "Failed opening file: " << this->filename << '\n';
|
||||
exit(1);
|
||||
}
|
||||
while (getline(file, bufferStr))
|
||||
{
|
||||
originalWordList.push_back(bufferStr);
|
||||
lineCount++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Main function for calling all sort categories
|
||||
void Sorter::RunSorts(void)
|
||||
{
|
||||
if (defaultOnly || fileGiven)
|
||||
{
|
||||
SetWordList();
|
||||
__RunSorts__();
|
||||
}
|
||||
if (allLists)
|
||||
SortAll();
|
||||
return;
|
||||
}
|
||||
|
||||
// Sorts all default files if allLists is set
|
||||
void Sorter::SortAll(void)
|
||||
{
|
||||
int fileCount = 10;
|
||||
std::string newFilename;
|
||||
for (int i = 1; i <= fileCount; i++)
|
||||
{
|
||||
lineCount = 0;
|
||||
newFilename = "test/PERM/perm";
|
||||
newFilename += std::to_string(15*i);
|
||||
newFilename += "K.txt";
|
||||
SetFilename(newFilename);
|
||||
SetWordList();
|
||||
__RunSorts__();
|
||||
originalWordList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Function for starting sort functions
|
||||
void Sorter::__RunSorts__(void)
|
||||
{
|
||||
if (!sortGiven)
|
||||
currentType = INSERTION;
|
||||
if (currentType == INSERTION)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
ImplementedSort::InsertionSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (!sortGiven)
|
||||
currentType = MERGE;
|
||||
if (currentType == MERGE)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
ImplementedSort::MergeSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (!sortGiven)
|
||||
currentType = HEAP;
|
||||
if (currentType == HEAP)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
ImplementedSort::HeapSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
}
|
||||
|
||||
// Main function for printing results
|
||||
void Sorter::OutputResult(void)
|
||||
{
|
||||
switch(currentType)
|
||||
{
|
||||
case INSERTION:
|
||||
PrintSortTime("IS");
|
||||
PrintToFile("IS");
|
||||
break;
|
||||
case MERGE:
|
||||
PrintSortTime("MS");
|
||||
PrintToFile("MS");
|
||||
break;
|
||||
case HEAP:
|
||||
PrintSortTime("HS");
|
||||
PrintToFile("HS");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Prints sort time to the screen
|
||||
void Sorter::PrintSortTime(std::string outputFilename)
|
||||
{
|
||||
std::string sortSizeString = std::to_string(lineCount / 1000);
|
||||
std::cout << outputFilename << sortSizeString;
|
||||
std::cout << " took " << sortTime.count() << " s" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Prints result of sort operation to file
|
||||
void Sorter::PrintToFile(std::string outputFilename)
|
||||
{
|
||||
std::string sortSizeString = std::to_string(lineCount / 1000);
|
||||
std::string outputPath = "test/OUTPUT/";
|
||||
outputPath += outputFilename + sortSizeString + "K.txt";
|
||||
std::ofstream file(outputPath);
|
||||
if (!file.is_open())
|
||||
{
|
||||
std::cout << "Failed opening file\n";
|
||||
exit(1);
|
||||
}
|
||||
for (unsigned int i = 0; i < lineCount; i++)
|
||||
file << newWordList[i] << '\n';
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Checks for command line arguments
|
||||
void CheckArguments(int argc, char* arguments[], Sorter* sortObj)
|
||||
{
|
||||
std::string tempStr;
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
tempStr = arguments[i];
|
||||
if ((tempStr == "-a") || (tempStr == "--all"))
|
||||
{
|
||||
sortObj->defaultOnly = 0;
|
||||
sortObj->allLists = 1;
|
||||
|
||||
}
|
||||
if ((tempStr == "-f") || (tempStr == "--filename"))
|
||||
{
|
||||
sortObj->SetFilename(arguments[i + 1]);
|
||||
sortObj->defaultOnly = 0;
|
||||
sortObj->fileGiven = 1;
|
||||
}
|
||||
if ((tempStr == "-d") || (tempStr == "--default"))
|
||||
{
|
||||
sortObj->SetFilename("test/PERM/perm15K.txt");
|
||||
sortObj->defaultFile = 1;
|
||||
}
|
||||
if ((tempStr == "-s") || (tempStr == "--sort-type"))
|
||||
{
|
||||
sortObj->sortGiven = 1;
|
||||
tempStr = arguments[i + 1];
|
||||
if (tempStr == "insertion")
|
||||
sortObj->currentType = INSERTION;
|
||||
if (tempStr == "merge")
|
||||
sortObj->currentType = MERGE;
|
||||
if (tempStr == "heap")
|
||||
sortObj->currentType = HEAP;
|
||||
if (tempStr == "all")
|
||||
sortObj->sortGiven = 0;
|
||||
}
|
||||
}
|
||||
if (sortObj->defaultOnly)
|
||||
sortObj->SetFilename("test/PERM/perm15K.txt");
|
||||
return;
|
||||
}
|
||||
|
||||
namespace ImplementedSort
|
||||
{
|
||||
// Reimplementation of Insertion Sort to be standalone
|
||||
void InsertionSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
int i;
|
||||
std::string key;
|
||||
for (int j = 1; j < newWordList->size(); j++)
|
||||
{
|
||||
key = newWordList->at(j);
|
||||
i = j - 1;
|
||||
while ((i >= 0) && (newWordList->at(i) > key))
|
||||
{
|
||||
newWordList->at(i + 1) = newWordList->at(i);
|
||||
i = i - 1;
|
||||
}
|
||||
newWordList->at(i + 1) = key;
|
||||
}
|
||||
}
|
||||
|
||||
// Reimplementation of Merge Sort to be standalone
|
||||
void MergeSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
__MergeSort__(newWordList, 0, newWordList->size() - 1);
|
||||
return;
|
||||
}
|
||||
|
||||
void __MergeSort__(std::vector<std::string> *newWordList, int p, int r)
|
||||
{
|
||||
if (p < r)
|
||||
{
|
||||
int q = (p + r) / 2;
|
||||
__MergeSort__(newWordList, p, q);
|
||||
__MergeSort__(newWordList, q+1, r);
|
||||
__Merge__(newWordList, p, q, r);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void __Merge__(std::vector<std::string> *newWordList, int p, int q, int r)
|
||||
{
|
||||
int n1 = q - p + 1;
|
||||
int n2 = r - q;
|
||||
const int leftTmpSize = n1 + 1;
|
||||
const int rightTmpSize = n2 + 1;
|
||||
std::string leftTmp[leftTmpSize];
|
||||
std::string rightTmp[rightTmpSize];
|
||||
for (int i = 0; i < n1; i++)
|
||||
leftTmp[i] = newWordList->at(p + i);
|
||||
for (int i = 0; i < n2; i++)
|
||||
rightTmp[i] = newWordList->at(q + i + 1);
|
||||
leftTmp[n1] = "ZZZZZ";
|
||||
rightTmp[n2] = "ZZZZZ";
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
for (int k = p; k <= r; k++)
|
||||
{
|
||||
if (leftTmp[i] <= rightTmp[j])
|
||||
{
|
||||
newWordList->at(k) = leftTmp[i];
|
||||
i++;
|
||||
} else {
|
||||
newWordList->at(k) = rightTmp[j];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Reimplementation of Heap Sort to be standalone
|
||||
void HeapSort(std::vector<std::string> *newWordList)
|
||||
{
|
||||
__HeapSort__(newWordList);
|
||||
return;
|
||||
}
|
||||
|
||||
void __HeapSort__(std::vector<std::string> *newWordList)
|
||||
{
|
||||
std::string tempStr;
|
||||
int heapSize;
|
||||
// Build Max Heap
|
||||
heapSize = newWordList->size();
|
||||
for (int i = ((newWordList->size() / 2) - 1); i >= 0; i--)
|
||||
__Heapify__(newWordList, i, heapSize);
|
||||
// Heap Sort
|
||||
for (int i = (newWordList->size() - 1); i >= 0; i--)
|
||||
{
|
||||
tempStr = newWordList->at(i);
|
||||
newWordList->at(i) = newWordList->at(0);
|
||||
newWordList->at(0) = tempStr;
|
||||
heapSize = i;
|
||||
__Heapify__(newWordList, 0, heapSize);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void __Heapify__(std::vector<std::string> *newWordList, int i, int heapSize)
|
||||
{
|
||||
std::string tempStr;
|
||||
int largest = i;
|
||||
int leftIndex = __HEAPSORT_LEFT__(i);
|
||||
int rightIndex = __HEAPSORT_RIGHT__(i);
|
||||
if ((leftIndex < heapSize) && (newWordList->at(leftIndex) > newWordList->at(largest)))
|
||||
largest = leftIndex;
|
||||
if ((rightIndex < heapSize) && (newWordList->at(rightIndex) > newWordList->at(largest)))
|
||||
largest = rightIndex;
|
||||
if (largest != i)
|
||||
{
|
||||
tempStr = newWordList->at(i);
|
||||
newWordList->at(i) = newWordList->at(largest);
|
||||
newWordList->at(largest) = tempStr;
|
||||
__Heapify__(newWordList, largest, heapSize);
|
||||
}
|
||||
}
|
||||
|
||||
int __HEAPSORT_LEFT__(int i)
|
||||
{
|
||||
return ((2 * i) + 1);
|
||||
}
|
||||
|
||||
int __HEAPSORT_RIGHT__(int i)
|
||||
{
|
||||
return ((2 * i) + 2);
|
||||
}
|
||||
}
|
81
src/trees.cpp
Normal file
81
src/trees.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
#include "trees.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace tree_implementation
|
||||
{
|
||||
Tree::Tree(void)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void Tree::Insert(void)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void Tree::Search(void)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void Tree::InOrderTreeTraversal(void)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void BinarySearchTree::PrintParentKey(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void BinarySearchTree::PrintLeftChild(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void BinarySearchTree::PrintRightChild(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void BinarySearchTree::PrintPathToRoot(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintParentKey(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintLeftChild(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintRightChild(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintPathToRoot(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintColor(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintParentColor(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void RedBlackTree::PrintUncleColor(std::string key)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user