Data update

This commit is contained in:
Ingy döt Net 2025-06-11 20:16:52 -04:00
parent 72eb4943cb
commit 4d5544505c
2347 changed files with 62432 additions and 16731 deletions

View file

@ -1,18 +1,14 @@
proc sort . d[] .
proc sort &d[] .
# radix = 10
radix = 256
max = 0
max = -1 / 0
for di = 1 to len d[]
if d[di] > max
max = d[di]
.
max = higher d[di] max
.
len buck[][] radix
pos = 1
while pos <= max
for i = 1 to radix
len buck[i][] 0
.
for i = 1 to radix : len buck[i][] 0
for di = 1 to len d[]
h = d[di] div pos mod radix + 1
buck[h][] &= d[di]

View file

@ -0,0 +1,60 @@
// Radix sort comparator for 32-bit two's complement integers
class RadixTest {
constructor(bit) {
this.bit = bit; // bit position [0..31] to examine
}
// function call operator (simulated with a method)
test(value) {
if (this.bit === 31) { // sign bit
return value < 0; // negative int to left partition
} else {
return !(value & (1 << this.bit)); // 0 bit to left partition
}
}
}
// Helper function to partition an array in place
function partition(arr, start, end, radixTest) {
let i = start - 1;
for (let j = start; j < end; j++) {
if (radixTest.test(arr[j])) {
i++;
// Swap arr[i] and arr[j]
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
return i + 1;
}
// Least significant digit radix sort
function lsdRadixSort(arr) {
for (let lsb = 0; lsb < 32; ++lsb) { // least-significant-bit
const radixTest = new RadixTest(lsb);
partition(arr, 0, arr.length, radixTest);
}
}
// Most significant digit radix sort (recursive)
function msdRadixSort(arr, start = 0, end = arr.length, msb = 31) {
if (start < end -1 && msb >= 0) { // changed (first != last) to (start < end-1) for easier indexing
const radixTest = new RadixTest(msb);
let mid = partition(arr, start, end, radixTest);
msb--; // decrement most-significant-bit
msdRadixSort(arr, start, mid, msb); // sort left partition
msdRadixSort(arr, mid, end, msb); // sort right partition
}
}
// test radix_sort
function main() {
let data = [170, 45, 75, -90, -802, 24, 2, 66];
lsdRadixSort(data);
// msdRadixSort(data);
console.log(data.join(" "));
}
main();

View file

@ -6,13 +6,11 @@ fn merge(in1: &[i32], in2: &[i32], out: &mut [i32]) {
// least significant digit radix sort
fn radix_sort(data: &mut [i32]) {
for bit in 0..31 {
// types of small and big is Vec<i32>.
// It will be infered from the next call of merge function.
for bit in 0..i32::BITS - 1 {
let (small, big): (Vec<_>, Vec<_>) = data.iter().partition(|&&x| (x >> bit) & 1 == 0);
merge(&small, &big, data);
}
// last bit is sign
// lastly, handle the sign bit
let (negative, positive): (Vec<_>, Vec<_>) = data.iter().partition(|&&x| x < 0);
merge(&negative, &positive, data);
}

View file

@ -0,0 +1,24 @@
fn radix_sort(arr: &mut [u32]) {
let mut buf = vec![0; arr.len()];
// Optional: Reduce the amount of iterations by finding the max value bit
let max_bit = u32::BITS - arr.iter().max().map(|t| t.leading_zeros()).unwrap_or(0);
for bit in 0..max_bit {
// Manual implementation of partition with only one buffer array
let mut a = 0;
let mut b = 0;
for i in 0..arr.len() {
if arr[i] >> bit & 1 == 0 {
arr[a] = arr[i];
a += 1;
} else {
buf[b] = arr[i];
b += 1;
}
}
arr[a..].copy_from_slice(&buf[..b]);
}
}

View file

@ -0,0 +1,40 @@
fn radix_sort(arr: &mut [i32]) {
let mut buf = vec![0; arr.len()];
// Optional: Reduce the number of iterations by finding the max value bit
let max_bit = arr.iter().fold(0, |acc, x| {
let bits = if x.is_negative() {
i32::BITS - x.leading_ones() // Negative msb is always 1 so leading_ones() >= 1
} else {
i32::BITS - x.leading_zeros() // Positive/zero msb is always 0 so leading_zeros() >= 1
};
core::cmp::max(acc, bits)
});
let sign_bit = i32::BITS - 1;
// Manual implementation of partition with only one buffer array
let mut partition = |bit: u32| {
let mut a = 0;
let mut b = 0;
for i in 0..arr.len() {
// Flip the sign bit before comparing so negatives are always ordered before positives
if (arr[i] ^ i32::MIN) >> bit & 1 == 0 {
arr[a] = arr[i];
a += 1;
} else {
buf[b] = arr[i];
b += 1;
}
}
arr[a..].copy_from_slice(&buf[..b]);
};
// This will not duplicate work because max_bits <= sign_bit therefore bit < sign_bit
for bit in 0..max_bit {
partition(bit);
}
partition(sign_bit);
}