Search code examples
pythonsortingpython-3.xradix-sort

What is wrong with my radix sort?


Note: I am using python 3.

I am trying to sort a list of words in alphabetical order.

This is my sort:

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)

It is being used in this loop:

for x in range(0,maxL):
    radix_sort(results[x], x)

maxL is the length of the longest words I have, so iterating from 0 to maxL goes through the entire list.

My list results[] is a list of lists. Each list within results contains a word object described as follows:

class word(object): #object class

    def __init__(self, originalWord=None, azWord=None, wLength=None):
        self.originalWord = originalWord
        self.azWord = azWord
        self.wLength = wLength

For example, results[3] should contain a list of all words with wLength of 3.

When I feed my entire program the following input:

hello
world
alphabetical
dog
cat
potato
stack

With this piece of code:

for row in results:
    for item in row:
        print(item.originalWord)

It prints:

cat
cat
dog
dog
dog
cat
stack
stack
world
hello
hello
stack
hello
hello
world
hello
world
world
stack
stack
world
potato
potato
potato
potato
potato
potato
alphabetical

I'm pretty sure I'm iterating through results[] correctly when printing. Why is my radix_sort not giving me the right results? I tried using the debugger but no luck.

EDIT: I changed my code to be as follows:

def radix_sort(List, length):
    for i in range (length-1, -1, -1): 
        for word in List:  
            buckets = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
            index = ord(word.azWord[i])-ord('a')  
            buckets[index].append(word)   
            List[:] = []   
    for containedList in buckets:  
        List.extend(containedList)
    return List #returns an alphabetized list

Now it's giving me an error here:

for containedList in buckets:

It says "UnboundLocalError: local variable 'buckets' referenced before assignment". What does this mean?


Solution

  • Following on my comments, that should look like

    def radix_sort(List, length):
        for i in range (length-1, -1, -1):    #for every letter "column"
            # Here buckets are created for each iteration
            buckets = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
            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
            # Here List is reconstructed for each iteration
            List[:] = []
            for containedList in buckets:
                List.extend(containedList)