School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,007 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,008 people online right now. Registration is fast and FREE... Join Now!




Radix sorting(Python)

 

Radix sorting(Python)

brramiz

22 Oct, 2009 - 02:14 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 11


My Contributions
i've got my radix sort working property in all but one aspect.

i put in this list for aList:['2356', '2346', '2346', '124', '1234', '34587', '6780', '4567', '16379', '12', '3467']

and for some reason it tells me that my max length is 4 saying that 6780 is my max number, which it really isn't, 34587 is my largest number so why does it tell me that this is my max?

CODE

from queque import FrontZeroQueue
def radixSort(aList):
    bigQueue = FrontZeroQueue()
    newList = aList.split(",")
    print newList
    print max(newList)
    tot= len(max(newList))
    print tot
    passes = 1
    bins = []
    for num in xrange(10):
        q = FrontZeroQueue()
        bins.append(q)
    for nums in newList:
        bigQueue.enqueue(int(nums))
    while passes !=tot+1:
        while len(bigQueue) > 0:
            curNum=bigQueue.dequeue()
            if passes == 1:
                rem = curNum % 10
                bins[rem].enqueue(curNum)
            elif passes >=2:
                rem =curNum % (10**passes)
                rem = rem/ (10**(passes-1))
                bins[rem].enqueue(curNum)
    
        while not bins[0].isEmpty():
            bigQueue.enqueue(bins[0].dequeue())
        while not bins[1].isEmpty():
            bigQueue.enqueue(bins[1].dequeue())
        while not bins[2].isEmpty():
            bigQueue.enqueue(bins[2].dequeue())
        while not bins[3].isEmpty():
            bigQueue.enqueue(bins[3].dequeue())
        while not bins[4].isEmpty():
            bigQueue.enqueue(bins[4].dequeue())
        while not bins[5].isEmpty():
            bigQueue.enqueue(bins[5].dequeue())
        while not bins[6].isEmpty():
            bigQueue.enqueue(bins[6].dequeue())


This post has been edited by brramiz: 22 Oct, 2009 - 02:21 PM

User is offlineProfile CardPM
+Quote Post


code_m

RE: Radix Sorting(Python)

23 Oct, 2009 - 07:56 AM
Post #2

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 124



Thanked: 8 times
My Contributions
It's your list.... it only contains the str type, so normal "number comparisions" get confused.

You'll want to make the change to int before making comparisions
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:06AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month