From bf271f103d1979ea61ce384d251a6fefe96e3f11 Mon Sep 17 00:00:00 2001 From: seancyw Date: Tue, 7 Feb 2017 22:07:38 +0800 Subject: [PATCH] Implemented Counting Sort Algorithms --- Algorithms/SortAlgorithms.h | 37 +++++++++++++++++++++++++++++ Algorithms/main.cpp | 46 +++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Algorithms/SortAlgorithms.h b/Algorithms/SortAlgorithms.h index 4ffcefe..b966116 100644 --- a/Algorithms/SortAlgorithms.h +++ b/Algorithms/SortAlgorithms.h @@ -147,6 +147,43 @@ namespace sortAlgorithms }; } + template + void countingSort(std::vector & data, bool ascending) + { + if (data.size() == 0) { + throw std::exception("Nothing to sort!"); + return; + } + + //Get the maximum element in the list as the maximum counter + //range + int maximumElement = *std::max_element(data.begin(), data.end()); + std::vector counter(maximumElement + 1); + std::fill(counter.begin(), counter.end(), 0); + + //Store count for each element + for (int i = 0; i < data.size(); ++i) + ++counter[data[i]]; + + //Recalculate each counter[i] so that it contains the actual + //position of the elements + for (int i = 1; i < counter.size(); ++i) + counter[i] += counter[i - 1]; + + //Initialize a temporary array to store the sorted array + std::vector temp( data.size() ); + for (int i = 0; i < data.size(); ++i) { + temp[counter[data[i]] - 1] = data[i]; + --counter[data[i]]; + } + + //Copy the sort array back to original array + data = temp; + + if (!ascending) + std::reverse(data.begin(), data.end()); + } + template class MergeSort { diff --git a/Algorithms/main.cpp b/Algorithms/main.cpp index 254dee3..875f1b7 100644 --- a/Algorithms/main.cpp +++ b/Algorithms/main.cpp @@ -14,7 +14,8 @@ enum testCase INSERTION_SORT, SHELL_SORT, MERGE_SORT, - QUICK_SORT + QUICK_SORT, + COUNTING_SORT }; void linearSearchTest(); @@ -25,10 +26,11 @@ void insertionSortTest(); void shellSortTest(); void mergeSortTest(); void quickSortTest(); +void countingSortTest(); int main() { - testCase test = QUICK_SORT; + testCase test = COUNTING_SORT; switch (test) { @@ -64,6 +66,10 @@ int main() quickSortTest(); break; + case COUNTING_SORT: + countingSortTest(); + break; + default: break; } @@ -552,5 +558,41 @@ void quickSortTest() std::cout << "\nAfter sorting in descending order, the lists contains: "; quickDoubleSort.printArray(); + std::cout << "\n"; +} + +void countingSortTest() +{ + std::cout << "Counting sort test\n\n"; + + //Initialize a empty vector to store value + std::vector intList; + std::vector doubleList; + + //initialize random seeds + srand((unsigned int)time(NULL)); + + //Push value to the list + std::cout << "list contains the followings elements: \n"; + int randomValue; + for (int iter = 0; iter < 20; ++iter) { + randomValue = rand() % 250 + 1; + intList.push_back(randomValue); + std::cout << randomValue << " "; + } + std::cout << "\n"; + + //Sort the list in ascending order + sortAlgorithms::countingSort(intList, true); + + std::cout << "\nAfter sorting in ascending order, the lists contains: "; + sortAlgorithms::printArray(intList); + + //Sort the list in descending order + sortAlgorithms::countingSort(intList, false); + + std::cout << "\nAfter sorting in descending order, the lists contains: "; + sortAlgorithms::printArray(intList); + std::cout << "\n"; } \ No newline at end of file