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 low;
float high; float high;
} ci; } ci;
int compare_doubles(const void* p, const void* q) typedef struct ci_searcher_t {
{ double num;
// https://wikiless.esmailelbob.xyz/wiki/Qsort?lang=en int remaining;
double x = *(const double*)p; } ci_searcher;
double y = *(const double*)q;
/* 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) ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
{ {
int n = 100 * 1000; 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++) { for (int i = 0; i < n; i++) {
samples_array[i] = sampler(seed); 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 = { ci result = {
.low = samples_array[5000], .low = samples_array[5000],