Add example for radix sort

This commit is contained in:
Shaun Reed 2021-05-29 14:23:23 -04:00
parent de0d706f98
commit 255d7efe9f
7 changed files with 196 additions and 0 deletions

View File

@ -23,3 +23,4 @@ add_subdirectory(heap)
add_subdirectory(quick) add_subdirectory(quick)
add_subdirectory(count) add_subdirectory(count)
add_subdirectory(bucket) add_subdirectory(bucket)
add_subdirectory(radix)

View File

@ -0,0 +1,19 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: A basic CMakeLists configuration to practice radix sort ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
cmake_minimum_required(VERSION 3.16)
project(RadixSort LANGUAGES CXX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(radix-sort "radix-sort.cpp")
add_library(lib-counting "lib-counting.cpp")
add_library(lib-radix "lib-radix.cpp")
target_link_libraries(radix-sort lib-radix lib-counting)

View File

@ -0,0 +1,65 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of counting sort using a custom library ##
## + In support of a radix sort implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#include "lib-counting.hpp"
#include <algorithm>
#include <vector>
#include <cstdint>
void CountingSort(std::vector<int> &array, int placeValue)
{
std::vector<int> sortedArray(array.size(), 0);
// 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 *placeValues* within range of the set
// + Not the size of the array to sort, but instead a element of
// + tempArray[digit] = 0; Considering only the digit at placeValue of array[n]
// Using this vector, we have a place to count all possible digits at placeValue
std::vector<int> tempArray(10, 0);
// Count the occurrences of each digit at this placeValue within the array
// + Store the end result as tempArray[digit] = count;
// + Where digit is found at the decimal placeValue of each value within array
for (const auto &val : array) {
// Get the digit at the decimal placeValue of arrayValue
auto index = static_cast<size_t>((val / placeValue) % tempArray.size());
// Running total of each value with the same digit at decimal placeValue
tempArray[index] = tempArray[index] + 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 (int i = 1; i < tempArray.size(); i++) {
tempArray[i] = tempArray[i] + tempArray[i - 1];
}
for (ssize_t arrayIndex = array.size() - 1; arrayIndex >= 0; arrayIndex--) {
// Get the value to consider from the unsorted array
const int &arrayValue = array[arrayIndex];
// Get the digit at the decimal placeValue of arrayValue
// + Use this index to update count of values as we use them
const auto tempIndex =
static_cast<size_t>((arrayValue / placeValue) % tempArray.size());
// Store valueCount as a reference so we can update it
int &valueCount = tempArray[tempIndex];
// Offset valueCount by 1 to allow for zero index of sortedArray
sortedArray[valueCount - 1] = arrayValue;
valueCount = valueCount - 1;
}
array = sortedArray;
}

View File

@ -0,0 +1,19 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of counting sort using a custom library ##
## + In support of a radix sort implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#ifndef RADIX_LIB_COUNT_HPP
#define RADIX_LIB_COUNT_HPP
#include <vector>
void CountingSort(std::vector<int> &array, int placeValue);
#endif // RADIX_LIB_COUNT_HPP

View File

@ -0,0 +1,29 @@
#include "lib-radix.hpp"
#include "lib-counting.hpp"
#include <algorithm>
#include <vector>
void RadixSort(std::vector<int> &array)
{
int maxValue = *std::max_element(array.begin(), array.end());
// Get the total number of digits in the maximum value within the set
// + 100 has 3 digits; 10 has 2 digits; etc..
int digits = 0;
for (int tempMax = maxValue; tempMax > 0; tempMax /= 10) digits++;
// For each placeValue of the maximum value within the set
for (int placeValue = 1; maxValue / placeValue > 0; placeValue *= 10) {
// Perform counting sort on all values, only considering the placeValue digit
// Result of sorting the first placeValue:
// + 12, 10, 15, 5, 3, 101, 109, 115 -> 10, 101, 12, 3, 115, 15, 5, 109
// Result of sorting the second placeValue:
// + 10, 101, 12, 3, 115, 15, 5, 109 -> 3, 5, 101, 109, 10, 12, 115, 15
// Result of sorting the third placeValue:
// + 3, 5, 101, 109, 10, 12, 115, 15 -> 3, 5, 10, 12, 15, 101, 109, 115
CountingSort(array, placeValue);
}
}

View File

@ -0,0 +1,18 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of radix sort using a custom library ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#ifndef LIB_RADIX_HPP
#define LIB_RADIX_HPP
#include <vector>
void RadixSort(std::vector<int> &array);
#endif // LIB_RADIX_HPP

View File

@ -0,0 +1,45 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of radix sort using a custom library ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#include "lib-counting.hpp"
#include "lib-radix.hpp"
#include <algorithm>
#include <vector>
#include <random>
#include <iostream>
// More values increase chance that the set provides colliding placeValue digits
#define ARRAY_LENGTH 20
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 150
// + This helps to provide values with 1, 2, or 3 total placeValue digits
// + Possible to get 0-9, 10-99, and 100-149
std::generate(array.begin(), array.end(), [](){ return rand() % 150;});
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);
RadixSort(array);
std::cout << "Sorted array: \n";
print(array);
}