2022-12-10 03:13:16 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2022-12-10 11:04:53 +01:00
|
|
|
// ChatGPT and me did this space partitioning bucket sort
|
2022-12-10 03:13:16 +01:00
|
|
|
void gpt_bucket_sort(uint32_t* array, int n) {
|
|
|
|
|
// Calculate the number of buckets to use
|
|
|
|
|
int num_buckets = std::sqrt(n);
|
|
|
|
|
|
|
|
|
|
// Create a vector of buckets
|
|
|
|
|
std::vector<std::vector<uint32_t>> buckets(num_buckets);
|
|
|
|
|
|
|
|
|
|
// Calculate the range of values that each bucket can hold
|
|
|
|
|
uint32_t min_value = *std::min_element(array, array + n);
|
|
|
|
|
uint32_t max_value = *std::max_element(array, array + n);
|
|
|
|
|
uint32_t range = max_value - min_value + 1;
|
|
|
|
|
uint32_t bucket_size = range / num_buckets + 1;
|
|
|
|
|
|
|
|
|
|
// Distribute the elements of the array into the buckets
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
// Calculate the bucket index for this element
|
|
|
|
|
// using the range of values and the bucket size as the divisor
|
|
|
|
|
int bucket_index = (array[i] - min_value) / bucket_size;
|
|
|
|
|
buckets[bucket_index].push_back(array[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort the elements in each bucket using std::sort
|
|
|
|
|
for (int i = 0; i < num_buckets; i++) {
|
|
|
|
|
std::sort(buckets[i].begin(), buckets[i].end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Concatenate the buckets to get the sorted array
|
|
|
|
|
int k = 0;
|
|
|
|
|
for (int i = 0; i < num_buckets; i++) {
|
|
|
|
|
for (int j = 0; j < buckets[i].size(); j++) {
|
|
|
|
|
array[k++] = buckets[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-10 11:04:53 +01:00
|
|
|
|
|
|
|
|
// Further optimizations (no chatGPT)
|
|
|
|
|
void my_bucket_sort(uint32_t* array, int n) {
|
|
|
|
|
// Calculate the number of buckets to use
|
|
|
|
|
int num_buckets = std::sqrt(n);
|
|
|
|
|
|
|
|
|
|
// Create a vector of buckets
|
|
|
|
|
std::vector<std::vector<uint32_t>> buckets(num_buckets);
|
|
|
|
|
|
|
|
|
|
// Calculate the range of values that each bucket can hold
|
|
|
|
|
auto mm = std::minmax_element(array, array + n);
|
|
|
|
|
uint32_t min_value = *mm.first;
|
|
|
|
|
uint32_t max_value = *mm.second;
|
|
|
|
|
uint32_t range = max_value - min_value + 1;
|
|
|
|
|
uint32_t bucket_size = range / num_buckets + 1;
|
|
|
|
|
|
|
|
|
|
// Distribute the elements of the array into the buckets
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
// Calculate the bucket index for this element
|
|
|
|
|
// using the range of values and the bucket size as the divisor
|
|
|
|
|
int bucket_index = (array[i] - min_value) / bucket_size;
|
|
|
|
|
buckets[bucket_index].push_back(array[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort the elements in each bucket using std::sort
|
|
|
|
|
for (int i = 0; i < num_buckets; i++) {
|
|
|
|
|
std::sort(buckets[i].begin(), buckets[i].end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Concatenate the buckets to get the sorted array
|
|
|
|
|
int k = 0;
|
|
|
|
|
for (int i = 0; i < num_buckets; i++) {
|
|
|
|
|
for (int j = 0; j < buckets[i].size(); j++) {
|
|
|
|
|
array[k++] = buckets[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|