Skip to content

Commit

Permalink
add msd
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Feb 6, 2020
1 parent a2dbb97 commit 42cde7f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ Try to keep the interface and variable name consistent with the original book wh
* [BellmanFordSP](algs4/bellman_ford_sp.py)

* 5 STRING

* [LSD](algs4/lsd.py)

* [MSD](algs4/msd.py)
* [Quick3string](algs4/quick3_string.py)
## License

This code is released under MIT.
Expand Down
24 changes: 11 additions & 13 deletions algs4/lsd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Execution: python lsd.py input.txt
Execution: python lsd.py < input.txt
Data files: https://algs4.cs.princeton.edu/51radix/words3.txt
% python lsd.py words3.txt
% python lsd.py < words3.txt
all
bad
bed
Expand All @@ -17,31 +17,29 @@


class LSD:

R = 256
@classmethod
def sort(cls, a, w):
n = len(a)
R = 256
aux = ['' for _ in range(n)]
for d in range(w-1, -1, -1):
count = [0 for _ in range(R+1)]
count = [0 for _ in range(cls.R+1)]
for i in range(n):
count[ord(a[i][d])+1] += 1
for r in range(R):
for r in range(cls.R):
count[r+1] += count[r]
for i in range(n):
aux[count[ord(a[i][d])]] = a[i]
count[ord(a[i][d])] += 1
for i in range(n):
a[i] = aux[i]


if __name__ == '__main__':
import sys
lst = []
with open(sys.argv[1]) as fp:
for line in fp:
for x in line.split(' '):
lst.append(x.strip())
LSD.sort(lst, len(lst[0]))
for item in lst:
words = []
for line in sys.stdin:
words.extend(line.split())
LSD.sort(words, len(words[0]))
for item in words:
print(item)
63 changes: 63 additions & 0 deletions algs4/msd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Execution: python msd.py < input.txt
Data files: https://algs4.cs.princeton.edu/51radix/words3.txt
% python msd.py < words3.txt
all
bad
bed
bug
dad
...
yes
yet
zoo
"""


class MSD:
R = 256 # extended ASCII alphabet size
CUTOFF = 15 # cutoff to insertion sort

def __init__(self, a):
self.aux = ["" for _ in range(len(a))]
self.sort(a, 0, len(a)-1, 0)

def sort(self, a, lo, hi, d):
if hi <= lo + self.CUTOFF:
self.insertion(a, lo, hi, d)
return

count = [0 for _ in range(self.R+2)]
for i in range(lo, hi+1):
count[self.char_at(a[i], d)+2] += 1
for r in range(self.R+1):
count[r+1] += count[r]
for i in range(lo, hi+1):
self.aux[count[self.char_at(a[i], d)+1]] = a[i]
count[self.char_at(a[i], d)+1] += 1
for i in range(lo, hi+1):
a[i] = self.aux[i-lo]
for r in range(self.R):
self.sort(a, lo+count[r], lo+count[r+1]-1, d+1)

def insertion(self, a, lo, hi, d):
for i in range(lo, hi+1):
j = i
while j > lo and a[j][d] < a[j-1][d]:
a[j], a[j-1] = a[j-1], a[j]
j -= 1

def char_at(self, s, d):
return ord(s[d])


if __name__ == '__main__':
import sys
words = []
for line in sys.stdin:
words.extend(line.split())
MSD(words)
for item in words:
print(item)

0 comments on commit 42cde7f

Please sign in to comment.