-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlsd.rb
34 lines (28 loc) · 770 Bytes
/
lsd.rb
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
32
33
34
# Least-significant-digit first string sort of fixed-length ASCII strings,
# linear-time sort in typical cases.
# See examples in test/lsd_test.rb
module LSD
RADIX = 256
def self.sort(strings, length)
(length - 1).downto(0).each do |i|
counts = Array.new(RADIX + 1, 0)
# compute frequency counts
strings.each do |string|
ch = string[i].ord
counts[ch + 1] += 1
end
# transform counts into indices
(1..RADIX).each { |r| counts[r] += counts[r - 1] }
# distribute it
aux = Array.new(strings.length)
strings.each do |string|
ch = string[i].ord
aux[counts[ch]] = string
counts[ch] += 1
end
# and copy back
strings = aux
end
strings
end
end