klips/cpp/algorithms/sorting/count/lib-counting.cpp

54 lines
2.3 KiB
C++
Raw Permalink Normal View History

2021-05-29 14:23:01 +00:00
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
2021-05-29 14:23:01 +00:00
## About: An example implementation of counting sort using a custom library ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#include "lib-counting.hpp"
#include <algorithm>
#include <cstdint>
#include <vector>
void CountingSort(std::vector<int> &array)
{
std::vector<int> sortedArray(ARRAY_LENGTH);
// Find the maximum value within the array to sort
int32_t maxValue = INT32_MIN;
for (const auto &val : array) maxValue = (val > maxValue) ? val : maxValue;
// Create an array with element for all *values* within the range of the set
// + Not the size of the array to sort, but instead a element of
// + tempArray[val] = 0; for each val within the array we want to sort
// Add one to maxValue to make up for zero index; 0's are possible, too!
std::vector<int> tempArray(maxValue + 1, 0);
// Count the occurrences of each value within the array to sort
// + Store the end result as tempArray[value] = count;
for (const auto &val : array) tempArray[val] += 1;
// Count the values less than or equal to each element of tempArray
// + Since each element stores its own count, just add the count at index i-1
for (int32_t i = 1; i <= maxValue; i++) {
2021-05-29 14:23:01 +00:00
tempArray[i] += tempArray[i - 1];
// tempArray[i] - 1 now represents the sorted 0-index pos for each value i
2021-05-29 14:23:01 +00:00
}
// Step through the array from right-to-left and place each value in their pos
// + As we run into values lookup their position with tempArray[value]
2021-05-29 14:23:01 +00:00
for (ssize_t arrayIndex = array.size() - 1; arrayIndex >= 0; arrayIndex--) {
const int &arrayValue = array[arrayIndex];
// Store as reference; Changes to valueCount reflect within tempArray
2021-05-29 14:23:01 +00:00
int &valueCount = tempArray[arrayValue];
sortedArray[valueCount - 1] = arrayValue;
// Subtract from tempArray[arrayValue] to update position of next occurrence
2021-05-29 14:23:01 +00:00
valueCount = valueCount - 1;
}
array = sortedArray;
}