MCPcopy Create free account
hub / github.com/douchuan/algorithm / sort_i32

Method sort_i32

src/strings/lsd.rs:85–127  ·  view source on GitHub ↗
(a: &mut [i32])

Source from the content-addressed store, hash-verified

83 }
84
85 pub fn sort_i32(a: &mut [i32]) {
86 let BITS = 32;
87 let MASK = R_I32 - 1;
88 let w = BITS / BITS_PER_BYTE;
89
90 let n = a.len();
91 let mut aux = vec![0; n];
92 for d in 0..w {
93 // compute frequency counts
94 let mut count = [0; R_I32 + 1];
95 for it in a.iter().take(n) {
96 let c = *it >> (BITS_PER_BYTE * d) & MASK as i32;
97 count[c as usize + 1] += 1;
98 }
99
100 // compute cumulates
101 for r in 0..R_I32 {
102 count[r + 1] += count[r];
103 }
104
105 // for most significant byte, 0x80-0xFF comes before 0x00-0x7F
106 if d == w - 1 {
107 let shift1 = count[R_I32] - count[R_I32 / 2];
108 let shift2 = count[R_I32 / 2];
109 for it in count.iter_mut().take(R_I32 / 2) {
110 *it += shift1;
111 }
112 for it in count.iter_mut().take(R_I32).skip(R_I32 / 2) {
113 *it -= shift2;
114 }
115 }
116
117 // move data
118 for it in a.iter().take(n) {
119 let c = *it >> (BITS_PER_BYTE * d) & MASK as i32;
120 aux[count[c as usize]] = *it;
121 count[c as usize] += 1;
122 }
123
124 // copy back
125 a[..n].clone_from_slice(&aux[..n]);
126 }
127 }
128}

Callers

nothing calls this directly

Calls 2

lenMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected