2023-01-12 13:32:19 -06:00
|
|
|
#include "sorts.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-01-16 16:11:40 -06:00
|
|
|
Sorter::Sorter(std::string newFilename)
|
|
|
|
{
|
|
|
|
lineCount = 0;
|
|
|
|
currentType = INSERTION;
|
|
|
|
SetFilename(newFilename);
|
|
|
|
SetWordList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sorter::SetFilename(std::string newName)
|
2023-01-12 13:32:19 -06:00
|
|
|
{
|
|
|
|
this->filename = newName;
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:11:40 -06:00
|
|
|
std::string Sorter::GetFilename(void)
|
2023-01-12 13:32:19 -06:00
|
|
|
{
|
|
|
|
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))
|
2023-01-16 16:11:40 -06:00
|
|
|
{
|
|
|
|
originalWordList.push_back(bufferStr);
|
|
|
|
lineCount++;
|
|
|
|
}
|
2023-01-12 13:32:19 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:11:40 -06:00
|
|
|
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;
|
2023-01-17 11:55:57 -06:00
|
|
|
outputPath += std::to_string(lineCount / 1000);
|
2023-01-16 16:11:40 -06:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:32:19 -06:00
|
|
|
void Sorter::InsertionSort(void)
|
|
|
|
{
|
2023-01-17 11:55:57 -06:00
|
|
|
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;
|
|
|
|
}
|
2023-01-12 13:32:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sorter::MergeSort(void)
|
|
|
|
{
|
2023-01-19 13:04:06 -06:00
|
|
|
__MergeSort__(0, lineCount - 1);
|
|
|
|
return;
|
2023-01-12 13:32:19 -06:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:04:06 -06:00
|
|
|
void Sorter::__MergeSort__(int p, int r)
|
2023-01-17 11:55:57 -06:00
|
|
|
{
|
2023-01-19 13:04:06 -06:00
|
|
|
if (p < r)
|
|
|
|
{
|
|
|
|
int q = (p + r) / 2;
|
|
|
|
__MergeSort__(p, q);
|
|
|
|
__MergeSort__(q+1, r);
|
|
|
|
__Merge__(p, q, r);
|
|
|
|
}
|
|
|
|
return;
|
2023-01-17 11:55:57 -06:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:04:06 -06:00
|
|
|
void Sorter::__Merge__(int p, int q, int r)
|
2023-01-17 11:55:57 -06:00
|
|
|
{
|
2023-01-19 13:04:06 -06:00
|
|
|
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;
|
|
|
|
for (int i = 0; i < n1; i++)
|
|
|
|
leftTmp.push_back(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");
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
for (int k = p; k <= r; k++)
|
|
|
|
{
|
|
|
|
if (leftTmp[i] <= rightTmp[j])
|
|
|
|
{
|
|
|
|
newWordList[k] = leftTmp[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newWordList[k] = rightTmp[j];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:55:57 -06:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:32:19 -06:00
|
|
|
void Sorter::HeapSort(void)
|
|
|
|
{
|
|
|
|
;
|
2023-01-16 16:11:40 -06:00
|
|
|
}
|