(Jan-14-2021, 07:49 PM)Serafim Wrote: [quote='deanhystad' pid='135130' dateline='1610610557']I don't at all agree to your statement that "Recursion is something to avoid"
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.
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 resultThanks 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!
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))