[Python] Encountering problems while making a sorting algorithm in python 3

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am an inexperienced coder, and took up the challenge to make a sorting algorithm to sort lists. I see people using the mod operator, and didn't like it, so I am using strings to make it a little more challenging. But, now I don't know how to fix it. Also, this is a Radix sorting algorithm, fyi

    #use list of numbers to sort
    Mylist = [122,123,124,125,456,457,458,789,790,791]
    #define empty spaces using s0-9
    print(Mylist)
    s0 = 0
    s1 = 0
    s2 = 0
    s3 = 0
    s4 = 0
    s5 = 0
    s6 = 0
    s7 = 0
    s8 = 0
    s9 = 0
    #function for getting a digit
    def getDigit(num, loc):
    digits = [int(digit) for digit in str(num)]
    return(digits[loc-1])

    #function for radix sorting a list
    def radix(l):
    if type(l) is not list:
    print("ERR: The variable is not a list")
    return
    for i in l:
    print(i, l.index(i))
    print("initiating LSD identification")
    SetDigitValues(l, 1)

    #function for assigning numbers in a list to s0-9, lining by LSD first
    def SetDigitValues(l, j):
    #check if least significant digit location is valid
    if j < 1:
    print("ERR: SetDigitValues was provided an invalid second element (least significant digit)")
    return
    #check if list was valid
    if type(l) is not list:
    print("ERR: The variable is not a list")
    return
    #define all used elements outside of function so it can see and change them
    global s0
    global s1
    global s2
    global s3
    global s4
    global s5
    global s6
    global s7
    global s8
    global s9
    s0=0
    s1=0
    s2=0
    s3=0
    s4=0
    s5=0
    s6=0
    s7=0
    s8=0
    s9=0
    #get digit of specified integer using the digit location, sorting into s0-9
    for i in l:
    if getDigit(i, (len(str(i)) + 1) - j) == 0:
    s0 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 1:
    s1 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 2:
    s2 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 3:
    s3 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 4:
    s4 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 5:
    s5 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 6:
    s6 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 7:
    s7 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 8:
    s8 += 1
    elif getDigit(i, (len(str(i)) + 1)-j) == 9:
    s9 += 1
    #add s0-9 so that no list locations overlap
    s1 = s0 + s1
    s2 = s1 + s2
    s3 = s2 + s3
    s4 = s3 + s4
    s5 = s4 + s5
    s6 = s5 + s6
    s7 = s6 + s7
    s8 = s7 + s8
    s9 = s8 + s9
    #make a new list to put new numbers in
    newlist = []
    h = 0
    while h < s9:
    newlist.append(0)
    h+=1
    #detect what number goes where in list using s0-9 identifications
    for g in l:
    if getDigit(g, (len(str(g)) + 1) - j) == 0:
    newlist.pop(s0-1)
    newlist.insert(s0-1, g)
    s0-=1
    elif getDigit(g, len(str(g))-j) == 1:
    newlist.pop(s1-1)
    newlist.insert(s1-1, g)
    s1-=1
    elif getDigit(g, len(str(g))-j) == 2:
    newlist.pop(s2-1)
    newlist.insert(s2-1, g)
    s2-=1
    elif getDigit(g, len(str(g))-j) == 3:
    newlist.pop(s3-1)
    newlist.insert(s3-1, g)
    s3-=1
    elif getDigit(g, len(str(g))-j) == 4:
    newlist.pop(s4-1)
    newlist.insert(s4-1, g)
    s4-=1
    elif getDigit(g, len(str(g))-j) == 5:
    newlist.pop(s5-1)
    newlist.insert(s5-1, g)
    s5-=1
    elif getDigit(g, len(str(g))-j) == 6:
    newlist.pop(s6-1)
    newlist.insert(s6-1, g)
    s6-=1
    elif getDigit(g, len(str(g))-j) == 7:
    newlist.pop(s7-1)
    newlist.insert(s7-1, g)
    s7-=1
    elif getDigit(g, len(str(g))-j) == 8:
    newlist.pop(s8-1)
    newlist.insert(s8-1, g)
    s8-=1
    elif getDigit(g, len(str(g))-j) == 9:
    newlist.pop(s9-1)
    newlist.insert(s9-1, g)
    s9-=1
    print(s9)
    print(newlist)


    radix(Mylist)


    this returned:

    [790, 123, 122, 458, 457, 456, 0, 0, 789, 791] not what I expected at all, and definetley not sorted.

    Continue reading...

Compartilhe esta Página