Python Forum
sorting a list using unicodes acending order, no loops, no sort(), using recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting a list using unicodes acending order, no loops, no sort(), using recursion
#11
this is an assignment as i said before....
the code that i wrote down is:

def sortlist(lst, i, b_ord ):
        
    if len(lst) == i:
        return lst
    
    elif b_ord > ord(lst[i]):
        
        lst[i - 1] = lst[i]
        lst[i] = chr(b_ord)
        b_ord = ord(lst[0])
        i = 0
            
            
    else:
        b_ord = ord(lst[i])
    
    sortlist(lst, (i + 1), b_ord)


lst = list(input())
n=ord('0')
i=0

sortlist(lst,i,n)
print("".join(lst))
but it is reaching the limit of recursion, although when i try sorting a string on jupyter notebook it works..
so i need to fix this code...
Reply
#12
https://codegolf.stackexchange.com/quest...-recursion
Reply
#13
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'
Reply
#14
(Jun-23-2021, 03:24 PM)deanhystad Wrote: 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. One 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'

so i didnt understand, the code you posted seems inefficient as well...
how can make it efficient, i believe that this is the cause of my problem.
Reply
#15
How long is your list? Can you provide a sample list that causes your sort to crash? If the list is too long it will be impossible to sort using recursion without changing the recursion depth limit. My recursive bubble sort is limited to sqrt(1000) or 31 elements in length. No matter how you sort you cannot sort a list greater than 1000 elements.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,488 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Count occurences in a string and add to a list using loops Leyo 4 1,686 Mar-11-2022, 03:52 PM
Last Post: Leyo
  Sorting list - Homework assigment ranbarr 1 2,230 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,899 May-14-2021, 07:14 AM
Last Post: perfringo
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,285 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  how to sort a list without .sort() function letmecode 3 3,440 Dec-28-2020, 11:21 PM
Last Post: perfringo
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,535 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Sorting nested lists in ascending order jszum 2 2,268 May-17-2020, 01:35 PM
Last Post: jefsummers
  Appending to a list in the right order Noobstudent 2 2,316 Dec-07-2019, 10:39 PM
Last Post: Noobstudent
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,719 Nov-14-2019, 02:44 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020