Jun-22-2021, 06:48 PM
(This post was last modified: Jun-22-2021, 07:44 PM by deanhystad.)
I doubt this is in the "spirit" of the challenge, but it doesn't call sort or use any loops.
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.
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?
def recursive_sort(items): if len(items) > 1: min_value = min(items) items.remove(min_value) return [min_value] + recursive_sort(items) return itemsIt 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 itemsThis 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?