Jun-23-2021, 03:24 PM
(This post was last modified: Jun-24-2021, 02:13 PM by deanhystad.)
It works fine for me. Why are you using the ord() function? Python uses a character's ordinal value when comparing, so there is no need for a conversion. One thing I would change is move the initializer code inside the function to make it easier to use.
def sortlist(lst, i=None, b=None): if i is None: sortlist(lst, 1, lst[0]) elif i < len(lst): if b > lst[i]: lst[i - 1] = lst[i] lst[i] = b b = lst[0] i = 0 else: b = lst[i] sortlist(lst, (i + 1), b)I added some print statements to get a better idea how your sort works and I noticed that it can be quite inefficient.
def sortlist(lst, i=None, b=None, count=None): print(lst, i, b, count) if i is None: sortlist(lst, 1, lst[0], 0) elif i < len(lst): if b > lst[i]: lst[i - 1] = lst[i] lst[i] = b b = lst[0] i = 0 else: b = lst[i] sortlist(lst, (i + 1), b, count+1)Sorting 'kd465b21' calls sortlist() 83 times compared to 36 times for the bubble sort which is already considered really inefficient. The inefficiency results from restarting the loop any time you find a value that is out of order. On the other hand this also means that your sort is the fastest sort in the world if the values are already in sorted order. Sorting lists that are in inverse order is a real killer causing sortlist() to be called 91 times for '87654321'