Skip to content

Instantly share code, notes, and snippets.

@sujayy1983
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sujayy1983/b9a23f8383b6da24b9fe to your computer and use it in GitHub Desktop.
Save sujayy1983/b9a23f8383b6da24b9fe to your computer and use it in GitHub Desktop.
Radix sorting: A non-comparitive sorting
"""
@Author: Sujayyendhiren Ramarao Srinivasamurthi
@Description: Radix sort my own way.
"""
def radixsort( inArr ):
"""
:param inArr: Input array for sorting
:return: Result is a dict of arrays indexable from 0-9.
"""
resultDict = {0:[],1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}
tempDict = resultDict
maxInList = 0
base = 10
currentDiv = 1
for num in inArr:
if num > maxInList:
maxInList = num
for num in inArr:
bucket = resultDict [ (num/currentDiv) % base ]
bucket.append(num)
tempDict = resultDict
resultDict = {0:[],1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}
while True:
currentDiv *= 10
print tempDict
for x in range(10):
listBucket = tempDict[x]
for elem in listBucket:
bucket = resultDict[(elem/currentDiv) % base]
bucket.append(elem)
tempDict = resultDict
resultDict = {0:[],1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}
if(((maxInList/currentDiv) % base)==0):
break
return tempDict
if __name__ == '__main__':
arr = [2,50,100,40,90,70,777,555]
result = radixsort( arr )
for x in range(10) :
bucketList = result[x]
for elem in bucketList:
print elem,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment