Add example for counting sort
This commit is contained in:
parent
33843eaaa1
commit
a7d11c559e
|
@ -21,3 +21,4 @@ add_subdirectory(insertion)
|
|||
add_subdirectory(bubble)
|
||||
add_subdirectory(heap)
|
||||
add_subdirectory(quick)
|
||||
add_subdirectory(count)
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
###############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: A basic CMakeLists configuration to practice counting sort ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
###############################################################################
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(CountingSort LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
add_executable(counting-sort "counting-sort.cpp")
|
||||
|
||||
add_library(lib-counting "lib-counting.cpp")
|
||||
target_link_libraries(counting-sort lib-counting)
|
|
@ -0,0 +1,40 @@
|
|||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## 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 <iostream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
|
||||
int main(const int argc, const char * argv[])
|
||||
{
|
||||
srand(time(nullptr));
|
||||
std::vector<int> array(ARRAY_LENGTH);
|
||||
// For this example, we limit the maximum value to 10
|
||||
// + This helps to provide duplicates so we have > 1 count of some values
|
||||
std::generate(array.begin(), array.end(), [](){ return rand() % 10;});
|
||||
|
||||
auto print = [](std::vector<int> array) {
|
||||
for (const auto &i : array) {
|
||||
std::cout << i << ", ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
};
|
||||
|
||||
std::cout << "Unsorted array: \n";
|
||||
print(array);
|
||||
|
||||
CountingSort(array);
|
||||
|
||||
std::cout << "Sorted array: \n";
|
||||
print(array);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## 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 (size_t i = 1; i <= maxValue; i++) {
|
||||
tempArray[i] += tempArray[i - 1];
|
||||
}
|
||||
|
||||
for (ssize_t arrayIndex = array.size() - 1; arrayIndex >= 0; arrayIndex--) {
|
||||
// Store as references; Changes reflect on actual values within each array
|
||||
const int &arrayValue = array[arrayIndex];
|
||||
int &valueCount = tempArray[arrayValue];
|
||||
sortedArray[valueCount - 1] = arrayValue;
|
||||
valueCount = valueCount - 1;
|
||||
}
|
||||
|
||||
array = sortedArray;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: An example implementation of counting sort using a custom library ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
###############################################################################
|
||||
*/
|
||||
|
||||
#ifndef LIB_COUNT_HPP
|
||||
#define LIB_COUNT_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
// For this example double the size of the array so we have more values to count
|
||||
#define ARRAY_LENGTH 20
|
||||
|
||||
|
||||
void CountingSort(std::vector<int> &array);
|
||||
|
||||
#endif // LIB_COUNT_HPP
|
Loading…
Reference in New Issue