Separated sorts into separate namespace
This commit is contained in:
+139
-135
@@ -68,7 +68,7 @@ void Sorter::SortAll(void)
|
||||
for (int i = 1; i <= fileCount; i++)
|
||||
{
|
||||
lineCount = 0;
|
||||
newFilename = "test/SORTED/sorted";
|
||||
newFilename = "test/PERM/perm";
|
||||
newFilename += std::to_string(15*i);
|
||||
newFilename += "K.txt";
|
||||
SetFilename(newFilename);
|
||||
@@ -86,7 +86,10 @@ void Sorter::__RunSorts__(void)
|
||||
if (currentType == INSERTION)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
InsertionSort();
|
||||
auto start = std::chrono::system_clock::now();
|
||||
ImplementedSort::InsertionSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (!sortGiven)
|
||||
@@ -94,7 +97,10 @@ void Sorter::__RunSorts__(void)
|
||||
if (currentType == MERGE)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
MergeSort();
|
||||
auto start = std::chrono::system_clock::now();
|
||||
ImplementedSort::MergeSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (!sortGiven)
|
||||
@@ -102,7 +108,10 @@ void Sorter::__RunSorts__(void)
|
||||
if (currentType == HEAP)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
HeapSort();
|
||||
auto start = std::chrono::system_clock::now();
|
||||
ImplementedSort::HeapSort(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
}
|
||||
@@ -155,137 +164,6 @@ 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++)
|
||||
{
|
||||
key = newWordList[j];
|
||||
i = j - 1;
|
||||
while ((i >= 0) && (newWordList[i] > key))
|
||||
{
|
||||
newWordList[i + 1] = newWordList[i];
|
||||
i = i - 1;
|
||||
}
|
||||
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)
|
||||
{
|
||||
int q = (p + r) / 2;
|
||||
__MergeSort__(p, q);
|
||||
__MergeSort__(q+1, r);
|
||||
__Merge__(p, q, r);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Merge sort function child
|
||||
void Sorter::__Merge__(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[p + i];
|
||||
for (int i = 0; i < n2; i++)
|
||||
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++)
|
||||
{
|
||||
if (leftTmp[i] <= rightTmp[j])
|
||||
{
|
||||
newWordList[k] = leftTmp[i];
|
||||
i++;
|
||||
} else {
|
||||
newWordList[k] = rightTmp[j];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Main function for heap sort
|
||||
void Sorter::HeapSort(void)
|
||||
{
|
||||
auto start = std::chrono::system_clock::now();
|
||||
__HeapSort__();
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
return;
|
||||
}
|
||||
void Sorter::__HeapSort__(void)
|
||||
{
|
||||
std::string tempStr;
|
||||
// Build Max Heap
|
||||
heapSize = newWordList.size();
|
||||
for (int i = ((newWordList.size() / 2) - 1); i >= 0; i--)
|
||||
__Heapify__(i);
|
||||
// Heap Sort
|
||||
for (int i = (newWordList.size() - 1); i >= 0; i--)
|
||||
{
|
||||
tempStr = newWordList[i];
|
||||
newWordList[i] = newWordList[0];
|
||||
newWordList[0] = tempStr;
|
||||
heapSize = i;
|
||||
__Heapify__(0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void Sorter::__Heapify__(int i)
|
||||
{
|
||||
std::string tempStr;
|
||||
int largest = i;
|
||||
int leftIndex = __LEFT__(i);
|
||||
int rightIndex = __RIGHT__(i);
|
||||
if ((leftIndex < heapSize) && (newWordList[leftIndex] > newWordList[largest]))
|
||||
largest = leftIndex;
|
||||
if ((rightIndex < heapSize) && (newWordList[rightIndex] > newWordList[largest]))
|
||||
largest = rightIndex;
|
||||
if (largest != i)
|
||||
{
|
||||
tempStr = newWordList[i];
|
||||
newWordList[i] = newWordList[largest];
|
||||
newWordList[largest] = tempStr;
|
||||
__Heapify__(largest);
|
||||
}
|
||||
}
|
||||
|
||||
int Sorter::__LEFT__(int i)
|
||||
{
|
||||
return ((2 * i) + 1);
|
||||
}
|
||||
|
||||
int Sorter::__RIGHT__(int i)
|
||||
{
|
||||
return ((2 * i) + 2);
|
||||
}
|
||||
|
||||
// Checks for command line arguments
|
||||
void CheckArguments(int argc, char* arguments[], Sorter* sortObj)
|
||||
{
|
||||
@@ -328,3 +206,129 @@ void CheckArguments(int argc, char* arguments[], Sorter* sortObj)
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user