Search code examples
pythonsortingradix-sort

radix sort using python function


I am using this code for sorting list with radix sort in python

def radix(self, a):
        len_a = len(a)
        modulus = 10
        div = 1
        while True:
            '''DECLARATION OF BUCKETS'''
            new_list = [[], [], [], [], [], [], [], [], [], []]
            for value in a:
                least_digit = value % modulus
                least_digit /= div
                new_list[least_digit].append(value)
            modulus = modulus * 10
            div = div * 10

            if len(new_list[0]) == len_a:
                '''RETURN THE LIST WHEN SORTED'''
                return new_list[0]

            a = []

            rd_list_append = a.append

            for x in new_list:
                for y in x:
                    rd_list_append(y)

I'm not able to understand what these lines do

            a = []

            rd_list_append = a.append

            for x in new_list:
                for y in x:
                    rd_list_append(y)

I know how the for loops will work but what is rd_list_append(y) Please help me. I'm new to python.


Solution

  • Ok, thats why they tell you not to copy code from the internet for your homework.

    This portion of the code is basically modifying the original list to be partially sorted (one round of radix sort). You can think of 'rd_list_append ' as the pointer to append function of list 'a'.
    You can directly do a.append(y) in the loop as well.