Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,66 @@
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
// Get size of statically allocated array
#define ARR_LEN(ARR) (sizeof ARR / sizeof *ARR)
// Generate random number in the interval [M,N]
#define RAND_RNG(M,N) (M + rand() / (RAND_MAX / (N - M + 1) + 1));
static void swap(unsigned *a, unsigned *b) {
unsigned tmp = *a;
*a = *b;
*b = tmp;
}
/* sort unsigned ints */
static void rad_sort_u(unsigned *from, unsigned *to, unsigned bit)
{
if (!bit || to < from + 1) return;
unsigned *ll = from, *rr = to - 1;
for (;;) {
/* find left most with bit, and right most without bit, swap */
while (ll < rr && !(*ll & bit)) ll++;
while (ll < rr && (*rr & bit)) rr--;
if (ll >= rr) break;
swap(ll, rr);
}
if (!(bit & *ll) && ll < to) ll++;
bit >>= 1;
rad_sort_u(from, ll, bit);
rad_sort_u(ll, to, bit);
}
/* sort signed ints: flip highest bit, sort as unsigned, flip back */
static void radix_sort(int *a, const size_t len)
{
size_t i;
unsigned *x = (unsigned*) a;
for (i = 0; i < len; i++)
x[i] ^= INT_MIN;
rad_sort_u(x, x + len, INT_MIN);
for (i = 0; i < len; i++)
x[i] ^= INT_MIN;
}
int main(void)
{
srand(time(NULL));
int x[16];
for (size_t i = 0; i < ARR_LEN(x); i++)
x[i] = RAND_RNG(-128,127)
radix_sort(x, ARR_LEN(x));
for (size_t i = 0; i < ARR_LEN(x); i++)
printf("%d%c", x[i], i + 1 < ARR_LEN(x) ? ' ' : '\n');
}

View file

@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
// Get size of statically allocated array
#define ARR_LEN(ARR) (sizeof ARR / sizeof *ARR)
// Generate random number in the interval [M,N]
#define RAND_RNG(M,N) (M + rand() / (RAND_MAX / (N - M + 1) + 1));
void radix_sort(const size_t n, int *array) {
int *bit0_nums = (int*)malloc(n*sizeof(int));
int *bit1_nums = (int*)malloc(n*sizeof(int));
for (size_t ibit=0; ibit<8*sizeof(int)-1; ibit++) {
size_t nbit0 = 0;
size_t nbit1 = 0;
for (size_t i=0; i<n; i++) {
int bit = (array[i]>>ibit)&0b1;
if (bit == 1) {
bit1_nums[nbit1] = array[i];
nbit1++;
} else {
bit0_nums[nbit0] = array[i];
nbit0++;
}
}
for (size_t i=0; i<nbit0; i++) {
array[i] = bit0_nums[i];
}
for (size_t i=0; i<nbit1; i++) {
array[i+nbit0] = bit1_nums[i];
}
}
{
// Sign bit needs reverse sorting due to twos complement
int ibit = 8*sizeof(int)-1;
size_t nbit0 = 0;
size_t nbit1 = 0;
for (size_t i=0; i<n; i++) {
int bit = (array[i]>>ibit)&0b1;
if (bit == 1) {
bit1_nums[nbit1] = array[i];
nbit1++;
} else {
bit0_nums[nbit0] = array[i];
nbit0++;
}
}
for (size_t i=0; i<nbit1; i++) {
array[i] = bit1_nums[i];
}
for (size_t i=0; i<nbit0; i++) {
array[i+nbit1] = bit0_nums[i];
}
}
free(bit0_nums);
free(bit1_nums);
}
int main(void) {
srand(time(NULL));
int x[16];
for (size_t i = 0; i < ARR_LEN(x); i++) {
x[i] = RAND_RNG(-128,127)
}
radix_sort(ARR_LEN(x), x);
for (size_t i = 0; i < ARR_LEN(x); i++) {
printf("%d%c", x[i], i + 1 < ARR_LEN(x) ? ' ' : '\n');
}
}

View file

@ -0,0 +1,70 @@
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
// Get size of statically allocated array
#define ARR_LEN(ARR) (sizeof ARR / sizeof *ARR)
// Generate random number in the interval [M,N]
#define RAND_RNG(M,N) (M + rand() / (RAND_MAX / (N - M + 1) + 1));
void radix_sort(const size_t n, int *array) {
int *nums = (int*)malloc(2*n*sizeof(int));
for (size_t ibit=0; ibit<8*sizeof(int)-1; ibit++) {
size_t nnums[2] = {0, 0};
for (size_t i=0; i<n; i++) {
int bit = (array[i]>>ibit)&0b1;
nums[bit*n+nnums[bit]] = array[i];
nnums[bit]++;
}
// To make it easily extendable to more buckets
size_t offset = 0;
for (int ibuck=0; ibuck<2; ibuck++) {
for (size_t i=0; i<nnums[ibuck]; i++) {
array[i+offset] = nums[ibuck*n+i];
}
offset += nnums[ibuck];
}
}
{
// Sign bit needs reverse sorting due to twos complement
int ibit = 8*sizeof(int)-1;
size_t nnums[2] = {0, 0};
for (size_t i=0; i<n; i++) {
int bit = (array[i]>>ibit)&0b1;
nums[bit*n+nnums[bit]] = array[i];
nnums[bit]++;
}
// To make it easily extendable to more buckets
size_t offset = 0;
for (int ibuck=1; ibuck<2; ibuck++) {
for (size_t i=0; i<nnums[ibuck]; i++) {
array[i+offset] = nums[ibuck*n+i];
}
offset += nnums[ibuck];
}
for (int ibuck=0; ibuck<1; ibuck++) {
for (size_t i=0; i<nnums[ibuck]; i++) {
array[i+offset] = nums[ibuck*n+i];
}
offset += nnums[ibuck];
}
}
free(nums);
}
int main(void) {
srand(time(NULL));
int x[16];
for (size_t i = 0; i < ARR_LEN(x); i++) {
x[i] = RAND_RNG(-128,127)
}
radix_sort(ARR_LEN(x), x);
for (size_t i = 0; i < ARR_LEN(x); i++) {
printf("%d%c", x[i], i + 1 < ARR_LEN(x) ? ' ' : '\n');
}
}