I am trying to sort lists within a list in alphabetical order using radix sort. I need the lists to be sorted by one of the attributes of an object I've created.
Note: I cannot use built-in sorts - I have to write my own. I was not allowed to use defaultdict so I used lists.
I have a list called results[]. In results[x] for all of results[] I have another list containing words of length x. Those words are stored as word objects containing the original word (originalWord), the alphabetized word (azWord), and its length (wLength). e.g. dog, dgo, 3.
Since I have many, many words, I've decided the radix sort would be most efficient for my purposes. I am relatively new to Python so I'm having some trouble writing the code correctly. I have a rough outline of what it should look like but I would appreciate help with fixing the syntax.
I am planning on using radix_sort in a for loop that iterates through results[]. I have a variable, maxL, that stores the longest word I have (aka the number of lists in results).
for x in range(0,maxL):
radix_sort(results[x], x)
Here is my attempt at writing radix sort for strings. Please note that the azWord attribute is represented as a char list.
def radix_sort(List, length):
buckets = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
for i in range (0, length-1): #for every letter "column"
for word in List: #for every word
index = ord(word[i].azWord)-ord('a') #get the index of the word
buckets[index].append(word) #add word object to correct bucket
for containedList in buckets:
while(containedList):
#I'm having trouble here with emptying the lists back into the bins
EDIT: Also, since I don't want to run out of memory (this is being done for a very long list of words), should I be clearing some things as I go that I don't need?
Also, currently, Eclipse is giving me an error "Expected:: Expected::" for this line:
for i in range (0, length-1)
Current version:
def radix_sort(List, length):
buckets = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
for i in range (length-1, -1, -1): #for every letter "column"
for word in List: #for every word
index = ord(word.azWord[i])-ord('a') #get the index of the word
buckets[index].append(word) #add word object to correct bucket
List[:] = []
for containedList in buckets:
List.extend(containedList)