magyarsort/gptsort.h

77 lines
2.4 KiB
C
Raw Normal View History

#include <cmath>
#include <vector>
#include <algorithm>
// ChatGPT and me did this space partitioning bucket sort
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];
}
}
}
// 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];
}
}
}