Add example for bucket sort

master
Shaun Reed 3 years ago
parent a7d11c559e
commit de0d706f98

@ -22,3 +22,4 @@ add_subdirectory(bubble)
add_subdirectory(heap)
add_subdirectory(quick)
add_subdirectory(count)
add_subdirectory(bucket)

@ -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)

@ -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 <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main(const int argc, const char * argv[])
{
srand(time(nullptr));
std::vector<float> array(ARRAY_LENGTH);
std::generate(array.begin(), array.end(),
[](){ return static_cast<float>(rand() % 1000) * 0.1f;}
);
auto print = [](std::vector<float> 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);
}

@ -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 <algorithm>
#include <list>
#include <vector>
void BucketSort(std::vector<float> &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<std::list<float>> 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<float>(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<float> &bucket : bucketArray) {
bucket.sort();
// After sorting each bucket, rewrite values in-order to final sorted array
for (const auto &value : bucket) array[currentIndex++] = value;
}
}

@ -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 <list>
#include <vector>
// 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<float> &array);
#endif // LIB_BUCKET_HPP
Loading…
Cancel
Save