squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

commit dc3f7eed4d9fd279cbeeef591f4ab29f4b1c025e
parent 03ca3e3b0cd46834e859c7367901b511ce80ce81
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Wed, 29 Nov 2023 21:46:44 +0000

run formatter in quickselect

Diffstat:
Mscratchpad/quickselect/makefile | 6++++++
Mscratchpad/quickselect/quickselect.c | 103+++++++++++++++++++++++++++++++++++++++++--------------------------------------
2 files changed, 60 insertions(+), 49 deletions(-)

diff --git a/scratchpad/quickselect/makefile b/scratchpad/quickselect/makefile @@ -3,3 +3,9 @@ build: run: ./quickselect + +## Formatter +STYLE_BLUEPRINT=webkit +FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) +format: + $(FORMATTER) quickselect.c diff --git a/scratchpad/quickselect/quickselect.c b/scratchpad/quickselect/quickselect.c @@ -2,63 +2,68 @@ #include <stdio.h> #include <stdlib.h> -void swp(int i, int j, double xs[]){ - double tmp = xs[i]; - xs[i] = xs[j]; - xs[j] = tmp; +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"); +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++; +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; + 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; +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; +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; }