diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index 508f191..3b0f31d 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -22,3 +22,4 @@ add_subdirectory(bubble) add_subdirectory(heap) add_subdirectory(quick) add_subdirectory(count) +add_subdirectory(bucket) diff --git a/cpp/algorithms/sorting/bucket/CMakeLists.txt b/cpp/algorithms/sorting/bucket/CMakeLists.txt new file mode 100644 index 0000000..7e3a0cb --- /dev/null +++ b/cpp/algorithms/sorting/bucket/CMakeLists.txt @@ -0,0 +1,17 @@ +############################################################################### +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: A basic CMakeLists configuration to practice bucket sort ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### + +cmake_minimum_required(VERSION 3.16) +project(BucketSort LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(bucket-sort "bucket-sort.cpp") + +add_library(lib-bucket "lib-bucket.cpp") +target_link_libraries(bucket-sort lib-bucket) diff --git a/cpp/algorithms/sorting/bucket/bucket-sort.cpp b/cpp/algorithms/sorting/bucket/bucket-sort.cpp new file mode 100644 index 0000000..df2e1f1 --- /dev/null +++ b/cpp/algorithms/sorting/bucket/bucket-sort.cpp @@ -0,0 +1,40 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of bucket sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-bucket.hpp" + +#include +#include +#include +#include + + +int main(const int argc, const char * argv[]) +{ + srand(time(nullptr)); + std::vector array(ARRAY_LENGTH); + std::generate(array.begin(), array.end(), + [](){ return static_cast(rand() % 1000) * 0.1f;} + ); + + auto print = [](std::vector array) { + for (const auto &i : array) { + std::cout << i << ", "; + } + std::cout << std::endl; + }; + + std::cout << "Unsorted array: \n"; + print(array); + + BucketSort(array); + + std::cout << "Sorted array: \n"; + print(array); +} diff --git a/cpp/algorithms/sorting/bucket/lib-bucket.cpp b/cpp/algorithms/sorting/bucket/lib-bucket.cpp new file mode 100644 index 0000000..e8cb365 --- /dev/null +++ b/cpp/algorithms/sorting/bucket/lib-bucket.cpp @@ -0,0 +1,42 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of bucket sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-bucket.hpp" + +#include +#include +#include + + +void BucketSort(std::vector &array) +{ + // For this example, we need an array of linked lists + // + Each value of this array will be referred to as a bucket + // + Each list stores the values that fall within the range of that bucket + std::vector> bucketArray(BUCKET_ARRAY_LENGTH); + + // Truncation to zero will always floor the result of division + // + If the bucket contains negative values, convert them to their absolutes + // + Run BucketSort on the abs values, then convert them back to negative + // ++ For a mixed-bag of negative and positive, track which values you absolute + // ++ Then only negate those values after BucketSort + for (const auto & value : array) { + bucketArray[value / static_cast(bucketArray.size())].push_back(value); + } + + // Track the last used index in the final sorted array; Starting at 0 + int currentIndex = 0; + // Sort each bucket in the bucketArray + for (std::list &bucket : bucketArray) { + bucket.sort(); + // After sorting each bucket, rewrite values in-order to final sorted array + for (const auto &value : bucket) array[currentIndex++] = value; + } + +} diff --git a/cpp/algorithms/sorting/bucket/lib-bucket.hpp b/cpp/algorithms/sorting/bucket/lib-bucket.hpp new file mode 100644 index 0000000..053db87 --- /dev/null +++ b/cpp/algorithms/sorting/bucket/lib-bucket.hpp @@ -0,0 +1,23 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of bucket sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#ifndef LIB_BUCKET_HPP +#define LIB_BUCKET_HPP + +#include +#include + +// For this example double the size of the array to increase chance of collision +#define ARRAY_LENGTH 20 +// Define how many buckets we want to use +#define BUCKET_ARRAY_LENGTH 10 + +void BucketSort(std::vector &array); + +#endif // LIB_BUCKET_HPP