thiersort apply fixes, my own qsort added to the algs, quicksort_fromto fix; thier still buggy on random data - but others seem to get handled by its quicksorts under the hood...

This commit is contained in:
Richard Thier 2023-06-30 18:00:44 +02:00
parent 9ac3a76209
commit 58176a89b6
2 changed files with 55 additions and 14 deletions

View File

@ -447,8 +447,8 @@ static inline void thiersort_apply(
// Update j and ni
// Now we must find what should be at new location j
// instead of what we swap in there from old j loc.
TSU32 j = ni;
TSU32 ni = sortres[j].i;
j = ni;
ni = sortres[j].i;
}
}
}
@ -622,7 +622,7 @@ static inline void ts_quicksort_fromto(
/* Can use inplace steps from now on */
ts_quicksort_inplace(dst, from, li, lt, reent_data);
ts_quicksort_inplace(dst, li + 1, to, lt, reent_data);
ts_quicksort_inplace(dst, li, to, lt, reent_data);
}
/** Internal code reuse (type-branches should be optimized out) */

View File

@ -394,6 +394,40 @@ void pagedsort(uint32_t *a, int n) {
free(pd);
}
// I measured this being faster than std::sort which is giga-lol wtf...
void thier_quicksort(uint32_t *arr, int n) {
// Prepare: O(n)
tselem *tarr = thiersort_prepare_array(
arr,
// union tskey (askey)(void *elem),
[] (void *elem) {
tskey k;
k.u = *((uint32_t *)elem);
return k;
},
4, // elemsize,
n, // length,
malloc);
// Quicksort by me
ts_quicksort_inplace(
tarr,
0, // from
n, // to
ts_lt_uint,
nullptr);
// Apply: O(n)
uint32_t tmp[1]; // needed for elem swaps
thiersort_apply(
tarr,
arr,
n,
4, // elemsize
tmp,
free);
}
void thiersort_uintkey8(uint32_t *arr, int n) {
// Prepare: O(n)
tselem *tarr = thiersort_prepare_array(
@ -408,28 +442,24 @@ void thiersort_uintkey8(uint32_t *arr, int n) {
n, // length,
malloc);
/*
for(uint32_t i = 0; i < n; ++i) {
printf("In: %d @%d\n", tarr[i].key.u, tarr[i].i);
}
*/
// Sort: O(n*loglogn on amortized on random input):
/*
thiersort8_uintkey(
tarr,
n,
malloc,
free);
*/
ts_quicksort_inplace(
tarr,
0, // from
n, // to
ts_lt_uint,
nullptr);
/*
for(uint32_t i = 0; i < n; ++i) {
printf("Out: %d @%d\n", tarr[i].key.u, tarr[i].i);
}
*/
// Apply: O(n)
uint32_t tmp[1]; // needed for elem swaps
@ -467,6 +497,8 @@ void measure_single(int n) {
int main(void) {
//int n = 100000000;
//int n = 10000000;
//int n = 10000;
//int n = 100;
int n = 65;
printf("Sorting %d elements:\n\n", n);
@ -519,11 +551,20 @@ int main(void) {
w = v;*/
measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); });
assert(w == expected);
//measure(inputtype, "magbuck", [&] { magyar_bucket_sort(&w[0], w.size()); });
//assert(w == expected);
/*
measure(inputtype, "magbuck", [&] { magyar_bucket_sort(&w[0], w.size()); });
assert(w == expected);
measure(inputtype, "magbuck2", [&] { magyar_bucket_sort2(&w[0], w.size()); });
assert(w == expected);
measure(inputtype, "thiersort_uintkey8", [&] { thiersort_uintkey8(&w[0], w.size()); });
*/
measure(inputtype, "qsmine", [&] { thier_quicksort(&w[0], w.size()); });
assert(w == expected);
measure(inputtype, "thier", [&] { thiersort_uintkey8(&w[0], w.size()); });
if(w != expected) {
for(uint32_t i = 0; i < n; ++i) {
assert(w[i] == expected[i]);
}
}
assert(w == expected);
/*
w = v;