sorting-algorithms/src/sorts.cpp

128 lines
2.3 KiB
C++
Raw Normal View History

#include "sorts.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
Sorter::Sorter(std::string newFilename)
{
lineCount = 0;
currentType = INSERTION;
SetFilename(newFilename);
SetWordList();
}
void Sorter::SetFilename(std::string newName)
{
this->filename = newName;
}
std::string Sorter::GetFilename(void)
{
return this->filename;
}
void Sorter::SetWordList(void)
{
std::string bufferStr;
std::ifstream file(this->filename);
if (!file.is_open())
{
std::cout << "Failed opening file\n";
exit(1);
}
while (getline(file, bufferStr))
{
originalWordList.push_back(bufferStr);
lineCount++;
}
return;
}
void Sorter::RunSorts(void)
{
newWordList = originalWordList;
InsertionSort();
OutputResult();
currentType = MERGE;
newWordList = originalWordList;
MergeSort();
OutputResult();
currentType = HEAP;
newWordList = originalWordList;
HeapSort();
OutputResult();
}
void Sorter::OutputResult(void)
{
switch(currentType)
{
case INSERTION:
PrintToFile("IS");
break;
case MERGE:
PrintToFile("MS");
break;
case HEAP:
PrintToFile("HS");
break;
default:
break;
}
}
void Sorter::PrintToFile(std::string outputFilename)
{
std::string outputPath = "test/OUTPUT/";
outputPath += outputFilename;
outputPath += std::to_string(lineCount / 1000);
outputPath += "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();
}
void Sorter::InsertionSort(void)
{
int i;
std::string key;
for (int j = 1; j < lineCount; j++)
{
key = newWordList[j];
i = j - 1;
while ((i >= 0) && (newWordList[i] > key))
{
newWordList[i + 1] = newWordList[i];
i = i - 1;
}
newWordList[i + 1] = key;
}
}
void Sorter::MergeSort(void)
{
;
}
void Sorter::__MergeSort__(void)
{
;
}
void Sorter::__Merge__(void)
{
;
}
void Sorter::HeapSort(void)
{
;
}