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
#4
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.

Providing pseudo code will be difficult as the algorithm is so simple that the only thing that is tricky is the recursion.
function minindex(items, start, end):
    if start == end
        retrun start

    index = minindex(items, start + 1, end)  <== The recursion

    if items[index] < items[start]
        return index
    else
        return start
This is a particularly stupid design. Not only is a non-recursive looping solution faster, but it is much easier to understand.
def minindex(a, i, n):
    min_ = i
    for i in range(i+1, n+1):
        if a[i] < a[min_]:
            min_ = i
    return min_
The code is so simple that there really isn't any need for the function at all and the loop can be included directly in the sorting routine.

I will leave the sort up to you. Know that the recursion for the sort works almost identical to the recursion for minindex. First you find the index of the smallest value and swap a[0] with a[smallest]. Repeat for a[1:] by calling the sort function recursively and increasing the start index. Eventually you will get to where the start index = length and you are done sorting.

One last note. Insertion sort is a replacement sort. It changes the order of element sin the original list. You will not be making any L2[] lists.
Reply


Messages In This Thread
RE: Combine Two Recursive Functions To Create One Recursive Selection Sort Function - by deanhystad - Jan-14-2021, 07:49 AM

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 879 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Recursive regular expressions in Python risu252 2 1,263 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,465 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 851 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 801 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,181 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 960 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,329 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,164 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