i have school project need sort kind of data types different sorting algorithms. radix sort works can't sort else integers. i'm not going add sorting results else integer since every data type sorted integers.
that said, i'd know if there better way convert strings integers? here's came with. didn't want outsmart python , tried use standard function as possible.
def chartohex(char): return hex(ord(char))[2:] def stringtohex(text): t = '' char in text: t += chartohex(char) return t def stringtoint(text): return int(stringtohex(text), 16) print stringtoint('allo') print stringtoint('allp') print stringtoint('all') it work i'd happy know if there better way handle that. it's worth, sorting else integers radix sort sounds pointless. because if can sort list of integers. you'll have values keys list.
i had in mind that. each value in list, integer key. put key inside hashtable , value in list hash table. replace value in list integer key , sort list of keys.
for each keys in sorted list, list of value key , pop 1 item. put item inside list , continue.
i'd know if there way optimize process in way make worth using radix sort instead of other sort doesn't require conversion. amount of item in list may go beyond 50000.
edit
actually code here doesn't work strings of different sizes. i'm not sure how check that. padding strings space seems work.
def getmaxlen(ls): lenght = 0 text in ls: lenght = max(lenght, len(text)) return lenght def convertlist(ls): size = getmaxlen(ls) copy = ls[:] i, val in enumerate(copy): copy[i] = stringtoint(val.ljust(size, ' ')) return copy print convertlist(["allo", "all", "bal"])
first, take @ this article. article shows yes, in cases, can figure out radix sort algorithm strings faster other sort.
second, , more importantly, i'd ask if doing premature optimization. sorting 50k items python's sort() function going incredibly fast. unless you're sure bottleneck in application wouldn't worry , use sort() function. if bottleneck, i'd make sure there isn't someway can avoid doing of these sorts (e.g. caching, algorithms work on unsorted data, etc.)
Comments
Post a Comment