Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct floatList {
float *list;
int size;
} *FloatList;
int floatcmp( const void *a, const void *b) {
if (*(const float *)a < *(const float *)b) return -1;
else return *(const float *)a > *(const float *)b;
}
float median( FloatList fl )
{
qsort( fl->list, fl->size, sizeof(float), floatcmp);
return 0.5 * ( fl->list[fl->size/2] + fl->list[(fl->size-1)/2]);
}
int main()
{
static float floats1[] = { 5.1, 2.6, 6.2, 8.8, 4.6, 4.1 };
static struct floatList flist1 = { floats1, sizeof(floats1)/sizeof(float) };
static float floats2[] = { 5.1, 2.6, 8.8, 4.6, 4.1 };
static struct floatList flist2 = { floats2, sizeof(floats2)/sizeof(float) };
printf("flist1 median is %7.2f\n", median(&flist1)); /* 4.85 */
printf("flist2 median is %7.2f\n", median(&flist2)); /* 4.60 */
return 0;
}

View file

@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ELEMENTS 1000000
/* Return the k-th smallest item in array x of length len */
double quick_select(int k, double *x, int len)
{
inline void swap(int a, int b)
{
double t = x[a];
x[a] = x[b], x[b] = t;
}
int left = 0, right = len - 1;
int pos, i;
double pivot;
while (left < right)
{
pivot = x[k];
swap(k, right);
for (i = pos = left; i < right; i++)
{
if (x[i] < pivot)
{
swap(i, pos);
pos++;
}
}
swap(right, pos);
if (pos == k) break;
if (pos < k) left = pos + 1;
else right = pos - 1;
}
return x[k];
}
int main(void)
{
int i, length;
double *x, median;
/* Initialize random length double array with random doubles */
srandom(time(0));
length = random() % MAX_ELEMENTS;
x = malloc(sizeof(double) * length);
for (i = 0; i < length; i++)
{
// shifted by RAND_MAX for negative values
// divide by a random number for floating point
x[i] = (double)(random() - RAND_MAX / 2) / (random() + 1); // + 1 to not divide by 0
}
if (length % 2 == 0) // Even number of elements, median is average of middle two
{
median = (quick_select(length / 2, x, length) + quick_select(length / 2 - 1, x, length / 2)) / 2;
}
else // select middle element
{
median = quick_select(length / 2, x, length);
}
/* Sanity testing of median */
int less = 0, more = 0, eq = 0;
for (i = 0; i < length; i++)
{
if (x[i] < median) less ++;
else if (x[i] > median) more ++;
else eq ++;
}
printf("length: %d\nmedian: %lf\n<: %d\n>: %d\n=: %d\n", length, median, less, more, eq);
free(x);
return 0;
}

View file

@ -0,0 +1,5 @@
length: 992021
median: 0.000473
<: 496010
>: 496010
=: 1