Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue