1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#![no_std]

#[inline]
pub fn sort_u8(array: &mut [u8]) {
    if array.len() < 2 {
    } else {
        let mut table: [usize; 256] = [0; 256];
        for value in array.iter() {
            unsafe {
                *table.get_unchecked_mut(*value as usize) += 1;
            }
        }
        let mut pos = 0;
        for (i, amount) in table.iter().enumerate().filter(|(_, &amount)| amount > 0) {
            let end = *amount as usize + pos;
            if *amount % 2 == 1 {
                unsafe {
                    *array.get_unchecked_mut(pos) = i as u8;
                }
                pos += 1;
            }
            while pos < end {
                unsafe {
                    *array.get_unchecked_mut(pos) = i as u8;
                    *array.get_unchecked_mut(pos + 1) = i as u8;
                }
                pos += 2;
            }
        }
    }
}