diff --git a/include/sorts.h b/include/sorts.h index d4734f4..be13600 100644 --- a/include/sorts.h +++ b/include/sorts.h @@ -14,6 +14,7 @@ private: int lineCount; std::vector originalWordList; std::chrono::duration sortTime; + int heapSize; // Heap Sort only public: Sorter(); std::vector newWordList; @@ -33,6 +34,10 @@ public: void __MergeSort__(int p, int r); void __Merge__(int p, int q, int r); void HeapSort(void); + void __HeapSort__(void); + void __Heapify__(int i); + int __LEFT__(int i); + int __RIGHT__(int i); }; void CheckArguments(int argc, char* arguments[], Sorter* sortObj); diff --git a/src/sorts.cpp b/src/sorts.cpp index 2086912..6686a17 100644 --- a/src/sorts.cpp +++ b/src/sorts.cpp @@ -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 diff --git a/test/SortTimes.txt b/test/SortTimes.txt index 1453cc2..c01c614 100644 --- a/test/SortTimes.txt +++ b/test/SortTimes.txt @@ -1,30 +1,30 @@ -IS15 took 2.06461 s -MS15 took 0.0154898 s -HS15 took 5.4e-08 s -IS30 took 8.38467 s -MS30 took 0.0324445 s -HS30 took 7.2e-08 s -IS45 took 18.9657 s -MS45 took 0.0506499 s -HS45 took 5.3e-08 s -IS60 took 34.2814 s -MS60 took 0.069865 s -HS60 took 4.7e-08 s -IS75 took 54.3642 s -MS75 took 0.0892599 s -HS75 took 6.4e-08 s -IS90 took 80.1745 s -MS90 took 0.110413 s -HS90 took 6e-08 s -IS105 took 111.424 s -MS105 took 0.130451 s -HS105 took 6.8e-08 s -IS120 took 149.645 s -MS120 took 0.15206 s -HS120 took 5.7e-08 s -IS135 took 196.821 s -MS135 took 0.169646 s -HS135 took 6.4e-08 s -IS150 took 250.343 s -MS150 took 0.194482 s -HS150 took 1.18e-07 s +IS15 took 0.000852521 s +MS15 took 0.0142171 s +HS15 took 0.0216508 s +IS30 took 0.00166257 s +MS30 took 0.0296301 s +HS30 took 0.0466942 s +IS45 took 0.0025043 s +MS45 took 0.0466391 s +HS45 took 0.0730432 s +IS60 took 0.00331285 s +MS60 took 0.0630209 s +HS60 took 0.10038 s +IS75 took 0.00414368 s +MS75 took 0.0804194 s +HS75 took 0.129151 s +IS90 took 0.00511297 s +MS90 took 0.09825 s +HS90 took 0.161143 s +IS105 took 0.00584393 s +MS105 took 0.121123 s +HS105 took 0.191739 s +IS120 took 0.00668049 s +MS120 took 0.136146 s +HS120 took 0.216863 s +IS135 took 0.00745027 s +MS135 took 0.152574 s +HS135 took 0.245755 s +IS150 took 0.00832467 s +MS150 took 0.175396 s +HS150 took 0.285888 s