don't make quickselect "destructive"; recompile
This commit is contained in:
parent
c3de336a5b
commit
95e4532c2c
examples
core
00_example_template
01_one_sample
02_time_to_botec
03_gcc_nested_function
04_gamma_beta
05_hundred_lognormals
more
00_example_template
01_sample_from_cdf
02_sample_from_cdf_beta
03_ci_beta
04_nuclear_war
05_burn_10kg_fat
06_nuclear_recovery
07_algebra
08_algebra_and_conversion
09_ergonomic_algebra
10_twitter_thread_example
11_billion_lognormals_paralell
12_time_to_botec_parallel
13_parallelize_min
14_check_confidence_interval
test
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -6,6 +6,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h> // memcpy
|
||||
|
||||
/* Parallel sampler */
|
||||
void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples)
|
||||
|
@ -105,15 +106,24 @@ static int partition(int low, int high, double xs[], int length)
|
|||
static double quickselect(int k, double xs[], int n)
|
||||
{
|
||||
// https://en.wikipedia.org/wiki/Quickselect
|
||||
|
||||
double *ys = malloc((size_t)n * sizeof(double));
|
||||
memcpy(ys, xs, (size_t)n * sizeof(double));
|
||||
// ^: don't make this operation "destructive"
|
||||
|
||||
int low = 0;
|
||||
int high = n - 1;
|
||||
for (;;) {
|
||||
if (low == high) {
|
||||
return xs[low];
|
||||
double result = ys[low];
|
||||
free(ys);
|
||||
return result;
|
||||
}
|
||||
int pivot = partition(low, high, xs, n);
|
||||
int pivot = partition(low, high, ys, n);
|
||||
if (pivot == k) {
|
||||
return xs[pivot];
|
||||
double result = ys[pivot];
|
||||
free(ys);
|
||||
return result;
|
||||
} else if (k < pivot) {
|
||||
high = pivot - 1;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue
Block a user