diff --git a/.gitignore b/.gitignore index f47b981..5762778 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ wordlists test/OUTPUT +test/SortTimes.txt bin/main.out build/*.o build/main.o diff --git a/include/sorts.h b/include/sorts.h index be13600..8448bb4 100644 --- a/include/sorts.h +++ b/include/sorts.h @@ -14,7 +14,6 @@ private: int lineCount; std::vector originalWordList; std::chrono::duration sortTime; - int heapSize; // Heap Sort only public: Sorter(); std::vector newWordList; @@ -29,17 +28,26 @@ public: void OutputResult(void); void PrintSortTime(std::string outputFilename); void PrintToFile(std::string outputFilename); - void InsertionSort(void); - void MergeSort(void); - 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); +namespace ImplementedSort +{ + // Performs Insertion Sort on given word list (vector of strings) + void InsertionSort(std::vector *newWordList); + + // Performs Merge Sort on given word list (vector of strings) + void MergeSort(std::vector *newWordList); + void __MergeSort__(std::vector *newWordList, int p, int r); + void __Merge__(std::vector *newWordList, int p, int q, int r); + + // Performs Heap Sort on given word list (vector of strings) + void HeapSort(std::vector *newWordList); + void __HeapSort__(std::vector *newWordList); + void __Heapify__(std::vector *newWordList, int i, int heapSize); + int __HEAPSORT_LEFT__(int i); + int __HEAPSORT_RIGHT__(int i); +} + #endif diff --git a/src/sorts.cpp b/src/sorts.cpp index 6686a17..1fc759d 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/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 *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 *newWordList) + { + __MergeSort__(newWordList, 0, newWordList->size() - 1); + return; + } + + void __MergeSort__(std::vector *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 *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 *newWordList) + { + __HeapSort__(newWordList); + return; + } + + void __HeapSort__(std::vector *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 *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); + } +} \ No newline at end of file diff --git a/test/SortTimes.txt b/test/SortTimes.txt deleted file mode 100644 index c01c614..0000000 --- a/test/SortTimes.txt +++ /dev/null @@ -1,30 +0,0 @@ -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