Finished Heap Sort

This commit is contained in:
2023-02-11 22:43:54 -06:00
parent 27aa8cfeef
commit 7098735815
3 changed files with 85 additions and 32 deletions
+50 -2
View File
@@ -68,7 +68,7 @@ void Sorter::SortAll(void)
for (int i = 1; i <= fileCount; i++)
{
lineCount = 0;
newFilename = "test/PERM/perm";
newFilename = "test/SORTED/sorted";
newFilename += std::to_string(15*i);
newFilename += "K.txt";
SetFilename(newFilename);
@@ -233,9 +233,57 @@ void Sorter::__Merge__(int p, int q, int r)
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