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,83 @@
with Ada.Text_IO;
procedure Radix_Sort is
type Integer_Array is array (Positive range <>) of Integer;
procedure Least_Significant_Radix_Sort (Data : in out Integer_Array; Base : Positive := 10) is
type Bucket is record
Count : Natural := 0;
Content : Integer_Array (Data'Range);
end record;
subtype Bucket_Index is Integer range -Base + 1 .. Base - 1;
type Bucket_Array is array (Bucket_Index) of Bucket;
procedure Append (To : in out Bucket; Item : Integer) is
begin
To.Count := To.Count + 1;
To.Content (To.Count) := Item;
end Append;
function Get_Nth_Digit (Value : Integer; N : Positive) return Integer is
Result : Integer := (Value / (Base ** (N - 1))) mod Base;
begin
if Value < 0 then
Result := -Result;
end if;
return Result;
end Get_Nth_Digit;
function Get_Maximum return Natural is
Result : Natural := 0;
begin
for I in Data'Range loop
if abs (Data (I)) > Result then
Result := abs (Data (I));
end if;
end loop;
return Result;
end Get_Maximum;
function Split (Pass : Positive) return Bucket_Array is
Buckets : Bucket_Array;
begin
for I in Data'Range loop
Append (To => Buckets (Get_Nth_Digit (Data (I), Pass)),
Item => Data (I));
end loop;
return Buckets;
end Split;
function Merge (Buckets : Bucket_Array) return Integer_Array is
Result : Integer_Array (Data'Range);
Current_Index : Positive := 1;
begin
for Sublist in Buckets'Range loop
for Item in 1 .. Buckets (Sublist).Count loop
Result (Current_Index) := Buckets (Sublist).Content (Item);
Current_Index := Current_Index + 1;
end loop;
end loop;
return Result;
end Merge;
Max_Number : Natural := Get_Maximum;
Digit_Count : Positive := 1;
begin
-- count digits of biggest number
while Max_Number > Base loop
Digit_Count := Digit_Count + 1;
Max_Number := Max_Number / Base;
end loop;
for Pass in 1 .. Digit_Count loop
Data := Merge (Split (Pass));
end loop;
end Least_Significant_Radix_Sort;
Test_Array : Integer_Array := (170, 45, 75, -90, -802, 24, 2, 66);
begin
Least_Significant_Radix_Sort (Test_Array, 4);
for I in Test_Array'Range loop
Ada.Text_IO.Put (Integer'Image (Test_Array (I)));
end loop;
Ada.Text_IO.New_Line;
end Radix_Sort;

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');
}
}

View file

@ -0,0 +1,49 @@
require "table2"
-- Counting sort of 'a' according to the digit represented by 'exp'.
local function count_sort(a, exp)
local n = #a
local output = table.rep(n, 0)
local count = table.rep(10, 0)
for i = 1, n do
local t = math.trunc(a[i] / exp) % 10
count[t + 1] += 1
end
for i = 1, 9 do count[i + 1] += count[i] end
for i = n, 1, -1 do
local t = math.trunc(a[i] / exp) % 10
output[count[t + 1]] = a[i]
count[t + 1] -= 1
end
for i = 1, n do a[i] = output[i] end
end
-- Sorts 'a' in place.
local function radix_sort(a)
-- Check for negative elements.
local min = a:min()
-- If there are any, increase all elements by -min.
if min < 0 then
for i = 1, #a do a[i] -= min end
end
-- Now get the maximum to know number of digits
local max = a:max()
-- Do counting sort for each digit
local exp = 1
while math.trunc(max / exp) > 0 do
count_sort(a, exp)
exp *= 10
end
-- If there were negative elements, reduce all elements by -min.
if min < 0 then
for i = 1, #a do a[i] += min end
end
end
local aa = {{4, 65, 2, -31, 0, 99, 2, 83, 782, 1}, {170, 45, 75, 90, 2, 24, -802, -66}}
for aa as a do
print($"Before: \{{a:concat(", ")}}")
radix_sort(a)
print($"After : \{{a:concat(", ")}}")
print()
end