Updated README.md, fixed merge sort, added checking output with bash script, and added sort arguments

This commit is contained in:
2023-02-11 19:22:37 -06:00
parent 266104fc4b
commit d544974809
9 changed files with 267 additions and 36 deletions
+3 -2
View File
@@ -1,8 +1,9 @@
#include "sorts.h"
int main(int argc, char** argv)
int main(int argc, char* argv[])
{
Sorter newSort(argv[1]);
Sorter newSort;
CheckArguments(argc, argv, &newSort);
newSort.RunSorts();
return 0;
}
+152 -29
View File
@@ -1,34 +1,41 @@
#include "sorts.h"
#include <iostream>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
Sorter::Sorter(std::string newFilename)
// Initialization function for Sorter class
Sorter::Sorter()
{
lineCount = 0;
currentType = INSERTION;
SetFilename(newFilename);
SetWordList();
defaultOnly = 0;
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\n";
std::cout << "Failed opening file: " << this->filename << '\n';
exit(1);
}
while (getline(file, bufferStr))
@@ -39,32 +46,81 @@ void Sorter::SetWordList(void)
return;
}
// Main function for calling all sort categories
void Sorter::RunSorts(void)
{
newWordList = originalWordList;
InsertionSort();
OutputResult();
currentType = MERGE;
newWordList = originalWordList;
MergeSort();
OutputResult();
currentType = HEAP;
newWordList = originalWordList;
HeapSort();
OutputResult();
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;
InsertionSort();
OutputResult();
}
if (!sortGiven)
currentType = MERGE;
if (currentType == MERGE)
{
newWordList = originalWordList;
MergeSort();
OutputResult();
}
if (!sortGiven)
currentType = HEAP;
if (currentType == HEAP)
{
newWordList = originalWordList;
HeapSort();
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:
@@ -72,12 +128,21 @@ void Sorter::OutputResult(void)
}
}
// 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;
outputPath += std::to_string(lineCount / 1000);
outputPath += "K.txt";
outputPath += outputFilename + sortSizeString + "K.txt";
std::ofstream file(outputPath);
if (!file.is_open())
{
@@ -89,8 +154,10 @@ void Sorter::PrintToFile(std::string outputFilename)
file.close();
}
// Main function for insertion sort
void Sorter::InsertionSort(void)
{
auto start = std::chrono::system_clock::now();
int i;
std::string key;
for (int j = 1; j < lineCount; j++)
@@ -104,14 +171,21 @@ void Sorter::InsertionSort(void)
}
newWordList[i + 1] = key;
}
auto end = std::chrono::system_clock::now();
sortTime = end - start;
}
// Main function for merge sort
void Sorter::MergeSort(void)
{
auto start = std::chrono::system_clock::now();
__MergeSort__(0, lineCount - 1);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
return;
}
// Merge sort function child
void Sorter::__MergeSort__(int p, int r)
{
if (p < r)
@@ -124,19 +198,21 @@ void Sorter::__MergeSort__(int p, int r)
return;
}
// Merge sort function child
void Sorter::__Merge__(int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
// L[i - 1] R[j - 1]
std::vector<std::string> leftTmp;
std::vector<std::string> rightTmp;
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.push_back(newWordList[p + i]);
leftTmp[i] = newWordList[p + i];
for (int i = 0; i < n2; i++)
rightTmp.push_back(newWordList[q + i + 1]);
leftTmp.push_back("ZZZZZ");
rightTmp.push_back("ZZZZZ");
rightTmp[i] = newWordList[q + i + 1];
leftTmp[n1] = "ZZZZZ";
rightTmp[n2] = "ZZZZZ";
int i = 0;
int j = 0;
for (int k = p; k <= r; k++)
@@ -145,16 +221,63 @@ void Sorter::__Merge__(int p, int q, int r)
{
newWordList[k] = leftTmp[i];
i++;
}
else
{
} else {
newWordList[k] = rightTmp[j];
j++;
}
}
}
// Main function for heap sort
void Sorter::HeapSort(void)
{
auto start = std::chrono::system_clock::now();
;
auto end = std::chrono::system_clock::now();
sortTime = end - start;
}
// 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->allLists = 1;
return;
}
if ((tempStr == "-f") || (tempStr == "--filename"))
{
sortObj->SetFilename(arguments[i + 1]);
sortObj->fileGiven = 1;
return;
}
if ((tempStr == "-d") || (tempStr == "--default"))
{
sortObj->SetFilename("test/PERM/perm15K.txt");
sortObj->defaultOnly = 1;
return;
}
if ((tempStr == "-s") || (tempStr == "--sort-type"))
{
sortObj->sortGiven = 1;
tempStr = arguments[i + 1];
std::cout << tempStr << '\n';
if (tempStr == "insertion")
sortObj->currentType = INSERTION;
if (tempStr == "merge")
sortObj->currentType = MERGE;
if (tempStr == "heap")
sortObj->currentType = HEAP;
if (tempStr == "all")
sortObj->sortGiven = 0;
}
}
sortObj->SetFilename("test/PERM/perm15K.txt");
sortObj->defaultOnly = 1;
return;
}