start working on using quickselect instead of sorting

This commit is contained in:
NunoSempere 2023-11-25 21:28:43 +00:00
parent 5f6cc0fe4f
commit 92abecc653

View File

@ -25,21 +25,11 @@ typedef struct ci_t {
float low;
float high;
} ci;
int compare_doubles(const void* p, const void* q)
{
// https://wikiless.esmailelbob.xyz/wiki/Qsort?lang=en
double x = *(const double*)p;
double y = *(const double*)q;
typedef struct ci_searcher_t {
double num;
int remaining;
} ci_searcher;
/* Avoid returning x - y, which can cause undefined behaviour
because of signed integer overflow. */
if (x < y)
return -1; // Return -1 if you want ascending, 1 if you want descending order.
else if (x > y)
return 1; // Return 1 if you want ascending, -1 if you want descending order.
return 0;
}
ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
{
int n = 100 * 1000;
@ -47,7 +37,16 @@ ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
for (int i = 0; i < n; i++) {
samples_array[i] = sampler(seed);
}
qsort(samples_array, n, sizeof(double), compare_doubles);
// 10% confidence interval: n/20, n - n/20
ci_searcher low = {.x = samples_array[0], .remaining = n/20) };
ci_searcher high = {.x = samples_array[0], .remaining = n-(n/20) };
// test with finding the lowest
for(int j=1; i<n; j++){
if(low.x > samples_array[i]){
low.x = samples_array[i];
}
}
ci result = {
.low = samples_array[5000],