Skip to content

Commit

Permalink
Implemented Counting Sort Algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
seancyw committed Feb 7, 2017
1 parent 8cf3c46 commit bf271f1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
37 changes: 37 additions & 0 deletions Algorithms/SortAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,43 @@ namespace sortAlgorithms
};
}

template<typename T>
void countingSort(std::vector<T> & 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<int> 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<T> 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<typename T>
class MergeSort
{
Expand Down
46 changes: 44 additions & 2 deletions Algorithms/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum testCase
INSERTION_SORT,
SHELL_SORT,
MERGE_SORT,
QUICK_SORT
QUICK_SORT,
COUNTING_SORT
};

void linearSearchTest();
Expand All @@ -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)
{
Expand Down Expand Up @@ -64,6 +66,10 @@ int main()
quickSortTest();
break;

case COUNTING_SORT:
countingSortTest();
break;

default:
break;
}
Expand Down Expand Up @@ -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<int> intList;
std::vector<double> 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";
}

0 comments on commit bf271f1

Please sign in to comment.