diff --git a/scratchpad/quickselect/makefile b/scratchpad/quickselect/makefile deleted file mode 100644 index 5bc70f0..0000000 --- a/scratchpad/quickselect/makefile +++ /dev/null @@ -1,11 +0,0 @@ -build: - gcc quickselect.c -lm -o quickselect - -run: - ./quickselect - -## Formatter -STYLE_BLUEPRINT=webkit -FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) -format: - $(FORMATTER) quickselect.c diff --git a/scratchpad/quickselect/quickselect b/scratchpad/quickselect/quickselect deleted file mode 100755 index 6e47f82..0000000 Binary files a/scratchpad/quickselect/quickselect and /dev/null differ diff --git a/scratchpad/quickselect/quickselect.c b/scratchpad/quickselect/quickselect.c deleted file mode 100644 index 6ddc4c2..0000000 --- a/scratchpad/quickselect/quickselect.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include - -void swp(int i, int j, double xs[]) -{ - double tmp = xs[i]; - xs[i] = xs[j]; - xs[j] = tmp; -} - -void array_print(double xs[], int n) -{ - printf("["); - for (int i = 0; i < n; i++) { - printf("%f, ", xs[i]); - } - printf("]\n"); -} - -int partition(int low, int high, double xs[], int length) -{ - // To understand this function: - // - see the note after gt variable definition - // - go to commit 578bfa27 and the scratchpad/ folder in it, which has printfs sprinkled throughout - int pivot = low + floor((high - low) / 2); - double pivot_value = xs[pivot]; - swp(pivot, high, xs); - int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */ - for (int i = low; i < high; i++) { - if (xs[i] < pivot_value) { - swp(gt, i, xs); - gt++; - } - } - swp(high, gt, xs); - return gt; -} - -double quickselect(int k, double xs[], int length) -{ - int low = 0; - int high = length - 1; - for (;;) { - if (low == high) { - return xs[low]; - } - int pivot = partition(low, high, xs, length); - if (pivot == k) { - return xs[pivot]; - } else if (k < pivot) { - high = pivot - 1; - } else { - low = pivot + 1; - } - } -} - -int main() -{ - double xs[] = { 2.1, 1.0, 6.0, 4.0, 7.0, -1.0, 2.0, 10.0 }; - int length = 8; - int k = 2; - array_print(xs, 8); - double result = quickselect(k, xs, length); - printf("The item in pos #%d is: %f\n", k, result); - array_print(xs, 8); - return 0; -} diff --git a/squiggle.c b/squiggle.c index 99caacb..f8a61ad 100644 --- a/squiggle.c +++ b/squiggle.c @@ -196,6 +196,16 @@ double array_std(double* array, int length) return std; } +void array_print(double xs[], int n) +{ + printf("["); + for (int i = 0; i < n - 1; i++) { + printf("%f, ", xs[i]); + } + printf("%f", xs[n - 1]); + printf("]\n"); +} + // Mixture function double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_dists, uint64_t* seed) { diff --git a/squiggle_more.c b/squiggle_more.c index 1ebadf4..1a63f99 100644 --- a/squiggle_more.c +++ b/squiggle_more.c @@ -30,6 +30,51 @@ typedef struct ci_searcher_t { int remaining; } ci_searcher; +void swp(int i, int j, double xs[]) +{ + double tmp = xs[i]; + xs[i] = xs[j]; + xs[j] = tmp; +} + +int partition(int low, int high, double xs[], int length) +{ + // To understand this function: + // - see the note after gt variable definition + // - go to commit 578bfa27 and the scratchpad/ folder in it, which has printfs sprinkled throughout + int pivot = low + floor((high - low) / 2); + double pivot_value = xs[pivot]; + swp(pivot, high, xs); + int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */ + for (int i = low; i < high; i++) { + if (xs[i] < pivot_value) { + swp(gt, i, xs); + gt++; + } + } + swp(high, gt, xs); + return gt; +} + +double quickselect(int k, double xs[], int length) +{ + int low = 0; + int high = length - 1; + for (;;) { + if (low == high) { + return xs[low]; + } + int pivot = partition(low, high, xs, length); + if (pivot == k) { + return xs[pivot]; + } else if (k < pivot) { + high = pivot - 1; + } else { + low = pivot + 1; + } + } +} + ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed) { int n = 100 * 1000;