little optimization to gpts and mine space partition bucket sort
This commit is contained in:
parent
7e21807668
commit
50b1997d5c
38
gptsort.h
38
gptsort.h
@ -2,6 +2,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
// ChatGPT and me did this space partitioning bucket sort
|
||||||
void gpt_bucket_sort(uint32_t* array, int n) {
|
void gpt_bucket_sort(uint32_t* array, int n) {
|
||||||
// Calculate the number of buckets to use
|
// Calculate the number of buckets to use
|
||||||
int num_buckets = std::sqrt(n);
|
int num_buckets = std::sqrt(n);
|
||||||
@ -36,3 +37,40 @@ void gpt_bucket_sort(uint32_t* array, int n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
3
makefile
3
makefile
@ -31,5 +31,8 @@ clang_release: test.cpp magyarsort.h
|
|||||||
clang_release3: test.cpp magyarsort.h
|
clang_release3: test.cpp magyarsort.h
|
||||||
clang++ test.cpp -DNDEBUG -std=c++17 -O3 -o test.out
|
clang++ test.cpp -DNDEBUG -std=c++17 -O3 -o test.out
|
||||||
|
|
||||||
|
clang_release_ypsu: ypsu.cpp magyarsort.h
|
||||||
|
clang++ ypsu.cpp -DNDEBUG -std=c++17 -O2 -o ypsu.out
|
||||||
|
|
||||||
clean: test.out
|
clean: test.out
|
||||||
rm test.out
|
rm test.out
|
||||||
|
|||||||
12
ypsu.cpp
12
ypsu.cpp
@ -399,7 +399,8 @@
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
std::vector<uint32_t> v(n);
|
std::vector<uint32_t> v(n);
|
||||||
v = geninput(inputtype, n);
|
v = geninput(inputtype, n);
|
||||||
measure(inputtype, "sp", [&] { spsort(&v[0], v.size()); });
|
//measure(inputtype, "sp", [&] { spsort(&v[0], v.size()); });
|
||||||
|
measure(inputtype, "magyar", [&] { MagyarSort::sort<uint32_t>(&v[0], v.size()); });
|
||||||
|
|
||||||
for (auto r : results) printf("%9.3fs", r.second);
|
for (auto r : results) printf("%9.3fs", r.second);
|
||||||
puts("");
|
puts("");
|
||||||
@ -414,8 +415,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int n = 100000000;
|
//int n = 100000000;
|
||||||
//int n = 10000000;
|
int n = 10000000;
|
||||||
//int n = 100;
|
//int n = 100;
|
||||||
|
|
||||||
// Uncomment this for profiling and alg!
|
// Uncomment this for profiling and alg!
|
||||||
@ -457,16 +458,17 @@
|
|||||||
w = v;
|
w = v;
|
||||||
measure(inputtype, "4pasu", [&] { fourpassu(&w[0], w.size()); });
|
measure(inputtype, "4pasu", [&] { fourpassu(&w[0], w.size()); });
|
||||||
assert(w == expected);
|
assert(w == expected);
|
||||||
*/
|
|
||||||
w = v;
|
w = v;
|
||||||
measure(inputtype, "4rot", [&] { fourrots(&w[0], w.size()); });
|
measure(inputtype, "4rot", [&] { fourrots(&w[0], w.size()); });
|
||||||
assert(w == expected);
|
assert(w == expected);
|
||||||
w = v;
|
w = v;
|
||||||
/*measure(inputtype, "sp", [&] { spsort(&w[0], w.size()); });
|
measure(inputtype, "sp", [&] { spsort(&w[0], w.size()); });
|
||||||
assert(w == expected);
|
assert(w == expected);
|
||||||
w = v;*/
|
w = v;*/
|
||||||
measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); });
|
measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); });
|
||||||
assert(w == expected);
|
assert(w == expected);
|
||||||
|
measure(inputtype, "mybuck", [&] { my_bucket_sort(&w[0], w.size()); });
|
||||||
|
assert(w == expected);
|
||||||
/*
|
/*
|
||||||
w = v;
|
w = v;
|
||||||
measure(inputtype, "frewr", [&] { frewr(&w[0], w.size()); });
|
measure(inputtype, "frewr", [&] { frewr(&w[0], w.size()); });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user