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
#9
I doubt this is in the "spirit" of the challenge, but it doesn't call sort or use any loops.
def recursive_sort(items):
    if len(items) > 1:
        min_value = min(items)
        items.remove(min_value)
        return [min_value] + recursive_sort(items)
    return items
It uses a sneak-cheat, borrowing the loop from min(). To make it less of a cheat you could do a recursive min.
def recursive_min(items):
    if len(items) > 1:
        a = items[0]
        b = recursive_min(items[1:])
        return a if a < b else b
    return items[0]

def recursive_sort(items):
    if len(items) > 1:
        min_value = recursive_min(items)
        items.remove(min_value)
        return [min_value] + recursive_sort(items)
    return items
This highlights what you need for a completely recursive solution: Two recursions.

Sorts have two loops; an inner loop to find the lowest or highest remaining value, and an outer loop to split the list into sorted and unsorted values. Sometimes the two are very obvious like the bubbler and index loops in the bubble sort, or the search and insert loops in the insertion sort. Sometimes they can be really difficult to see like the quick but confusing heap sort or quick sort. But there are always two loops. One loop doing the sorting and another loop deciding what to sort.

Knowing that all you are doing is replacing a loop with recursion, you can rewrite (probably) any sort by simply replacing the loops with recursively computing the "loop" indices. Here is a recursive bubble sort.
def recursive_bubble(items, i=None, j=None):
    if i is None:
        i = len(items)-1 # No items are sorted
        j = 0

    if i >= 0:
        if j < i:
            # bubble largest unsorted item to end
            if items[j] > items[j+1]:
                items[j], items[j+1] = items[j+1], items[j]
            recursive_bubble(items, i, j+1)
        else:
            # Move the unsorted/sorted pivot toward the front of the list
            recursive_bubble(items, i-1, 0)
The i "loop" splits the list into sorted and unsorted items. I starts at the end and moves toward the front of the list to eliminate multiple len() calls. The j "loop" is the bubbler. It pushes the largest remaining value toward the end of the list. Two loops. One to sort the remaining values (j) and one to decide which values need to be sorted (i).

Hopefully now you can see why your code does not work. Where are your "loops"? Where is the pivot loop? Where is the sorter loop?
Reply


Messages In This Thread
RE: sorting a list using unicodes acending order, no loops, no sort(), using recursion - by deanhystad - Jun-22-2021, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,637 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Count occurences in a string and add to a list using loops Leyo 4 1,765 Mar-11-2022, 03:52 PM
Last Post: Leyo
  Sorting list - Homework assigment ranbarr 1 2,288 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,985 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,421 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  how to sort a list without .sort() function letmecode 3 3,524 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,599 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Sorting nested lists in ascending order jszum 2 2,328 May-17-2020, 01:35 PM
Last Post: jefsummers
  Appending to a list in the right order Noobstudent 2 2,366 Dec-07-2019, 10:39 PM
Last Post: Noobstudent
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,851 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