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
#11
(Jan-14-2021, 07:49 PM)Serafim Wrote:
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
Hi Serafim, in your "for loop" in this code, what is the purpose of the "1" in "(1, len(lst))," because as I was studying the code to see how everything worked together to sort the list, I couldn't figure out what the purpose of the "1" was, so I deleted it to see if the code would still work, and it still works?
Reply
#12
(Jan-16-2021, 09:48 AM)Jeremy7 Wrote: Hi Serafim, in your "for loop" in this code, what is the purpose of the "1" in "(1, len(lst))," because as I was studying the code to see how everything worked together to sort the list, I couldn't figure out what the purpose of the "1" was, so I deleted it to see if the code would still work, and it still works?
range(len(lst)) means [0, 1, 2, ..., len(lst)-1] and range(1, len(lst)) starts at 1, giving [1, 2, ..., len(lst)-1] and as I use lst[0] as start value (element = lst[0]) I would, in the first step compare lst[0] < lst[0] if the loop was over range(len(lst)), which I know is false so I just skip that first step. It works with both constructs but my way skips one unnecessary test. Old programming habit, minimize work if obvious.
Reply
#13
(Jan-16-2021, 12:35 PM)Serafim Wrote: range(len(lst)) means [0, 1, 2, ..., len(lst)-1] and range(1, len(lst)) starts at 1, giving [1, 2, ..., len(lst)-1] and as I use lst[0] as start value (element = lst[0]) I would, in the first step compare lst[0] < lst[0] if the loop was over range(len(lst)), which I know is false so I just skip that first step. It works with both constructs but my way skips one unnecessary test. Old programming habit, minimize work if obvious.
Oooh! I see! It makes sense. Not only removing one unnecessary step, but making the code efficient, and I'm all about efficiency. I'm learning, and this learning curve is HUGE right now; I'll be glad when it shrinks to the size of a needle point. Just started learning Python in November 2020, and even though I attained an associate’s degree in computer programming in 2016, I had no clue then how code worked, it was just over my head, so it's like all new to me. And I was able to attain my degree because so many people helped me get through the courses. Sadly, I didn't learn anything in computer programming, except how variables worked; I just got through the programming courses. That's sad. So as you can see by me asking you about the "1" in (1, len(lst)), I had no clue how the code was actually working to sort the list, but with that simple explanation of why "1" is in there, now I see how it works; I see the WHOLE code; I see how all the dots connect. YAAAHOOO!!! Thanks again!
Reply


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 805 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Recursive regular expressions in Python risu252 2 1,121 Jul-25-2023, 12:59 PM
Last Post: risu252
  with open context inside of a recursive function billykid999 1 549 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,362 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 807 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 757 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,111 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 905 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,277 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,000 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