Python Forum
Combine Two Recursive Functions To Create One Recursive Selection Sort Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combine Two Recursive Functions To Create One Recursive Selection Sort Function
#9
(Jan-14-2021, 07:49 PM)Serafim Wrote: [quote='deanhystad' pid='135130' dateline='1610610557']
I do not understand your request. You talk about having 2 recursive functions, but you have no recursive functions. Recursion is when a function calls itself. Neither of your functions do that.

I don't know why you want a recursive sort. Recursion is something to avoid I am usually more interested in converting recursive solutions to not use recursion than the other way around. Maybe you want to do this task to make it easier to spot these dastardly recursive solutions.
I don't at all agree to your statement that "Recursion is something to avoid"

1. Jeremys request is an idea for how to combine his two functions into one that uses recursion.

2. He misses the point in calling his functions recursive, which they obviously are not but his request is nonetheless understandable, recursion is in itself interesting and often leads to shorter solutions than their iterative counterparts but they tend to be harder to debug.

3. In functional programming languages recursion is the only option for repetitive tasks. My examples are taken from my lectures (on the functional programming language Scheme) and transformed to Python, which in itself was an interesting task.

4. Also, when looking at the results, it is simple thinking behind: "How do we take one step towards a solution and how do we know when to stop?"

In the first solution, (a) pick the smallest element from the list and add it to the (initially empty) solution and (b) stop when there are no more elements.

In the second: (a) point at the next place (initially the first) to put the smallest remaining element (to make extra space unnecessary I use the "vacant" place as storage, hence the swap) and (b) stop when we have reached the last place.

In my mind it leads to elegant solutions and not "bastardly" (I guess that "dastardly" is a mistake) even if I still think that an iterative solution is more intuitive (following Jeremys idea of how to sort the elements into a new list):

def selection_sort(lst):
    result = []
    while lst:
        element = lst[0]
        for i in range(1, len(lst)):
            if lst[i] < element:
                element = lst[i]
        lst.remove(element)
        result.append(element)
    return result
Thanks again, Serafim; your code is pretty much right on what I was aiming for! Okay, so not all functions in Python that begin with "def" are recursive functions; I'm learning. What I should have done to make answering my question a whole lot easier was to take the code from the 2 "def" functions I initially posted, like I did in the code you see below, and ask how I would tweak it just enough to make it work while still looking a little the same. Sorry about that.
Again, thanks! You're my go-to guy for coding! Smile

def sortList(L,n):
    minValue = L[0]
    L2 = []
    idx = 0
    counter = 0
	
    while (counter < n):
        v = L[counter]
        if v < minValue:
            minValue = v
            idx = counter
            L2.append(minValue)
            del L[idx]
            n-=1

        counter += 1

    return L2

L = [34, -1, 0, 89, 21, -40, 7]
n = len(L)

print(sortList(L, n))
Reply


Messages In This Thread
RE: Combine Two Recursive Functions To Create One Recursive Selection Sort Function - by Jeremy7 - Jan-14-2021, 11:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i combine these two functions so i only open the file once? cubangt 4 876 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Recursive regular expressions in Python risu252 2 1,260 Jul-25-2023, 12:59 PM
Last Post: risu252
  with open context inside of a recursive function billykid999 1 589 May-23-2023, 02:37 AM
Last Post: deanhystad
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,458 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 849 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 798 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,179 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 959 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,150 Mar-26-2022, 03:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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