Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Steps
#9
Following your private message:

1. It seems a bit strange to me to check your sort method by using sorted(). If you have already sorted, why do it again?
2. Why make life difficult by adding 0 at the beginning? Add zero after sorting if you really need a 0 there.

Python implements timsort for sorting. Have a look here for various sorting methods and how efficient they are.

Quote:The Timsort Algorithm in Python
The Timsort algorithm is considered a hybrid sorting algorithm because it employs a best-of-both-worlds combination of insertion sort and merge sort. Timsort is near and dear to the Python community because it was created by Tim Peters in 2002 to be used as the standard sorting algorithm of the Python language.

nums = '5 1 3 4 2'
frog_list = [int(x) for x in nums.split()]
move_count = position = direction = 0
sorted_frogs = sorted(frog_list, reverse=True)
              
while frog_list != sorted_frogs:
    if direction == 0:
        if frog_list[position] == frog_list[-2]:
            frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
            position += 1
        elif frog_list[position] == frog_list[-1]:
            direction = 1
        elif frog_list[position + 2] > frog_list[position + 1]:
            frog_list[position], frog_list[position + 2] = frog_list[position + 2], frog_list[position]
            position += 2
        else:
            frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
            position += 1
    elif direction == 1:
        if frog_list[position] == frog_list[1]:
            frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
            position -= 1
        elif frog_list[position] == frog_list[0]:
            direction = 0
        elif frog_list[position - 2] < frog_list[position - 1]:
            frog_list[position], frog_list[position - 2] = frog_list[position - 2], frog_list[position]
            position -= 2
        else:
            frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
            position -= 1
       
    move_count += 1
    print(f'{frog_list}, moves = {move_count}, position = {position}, direction = {direction}')
You can see from the output that it works, but the 5 bounces around in the first 6 lines. Fix that if you can.

If you really need 0 do this after sorting:

frog_list.insert(0, 0)
MoreMoney likes this post
Reply


Messages In This Thread
Sorting Steps - by MoreMoney - Mar-26-2024, 02:16 PM
RE: Sorting Steps - by deanhystad - Mar-26-2024, 02:27 PM
RE: Sorting Steps - by MoreMoney - Mar-26-2024, 03:22 PM
RE: Sorting Steps - by deanhystad - Mar-26-2024, 03:24 PM
RE: Sorting Steps - by MoreMoney - Mar-26-2024, 03:40 PM
RE: Sorting Steps - by deanhystad - Mar-26-2024, 04:09 PM
RE: Sorting Steps - by MoreMoney - Mar-26-2024, 04:42 PM
RE: Sorting Steps - by deanhystad - Mar-27-2024, 02:39 AM
RE: Sorting Steps - by Pedroski55 - Mar-27-2024, 09:34 AM
RE: Sorting Steps - by deanhystad - Mar-27-2024, 06:14 PM
RE: Sorting Steps - by MoreMoney - Mar-28-2024, 07:30 AM
RE: Sorting Steps - by deanhystad - Mar-28-2024, 04:47 PM
RE: Sorting Steps - by Pedroski55 - Mar-29-2024, 09:48 AM
RE: Sorting Steps - by deanhystad - Mar-29-2024, 06:20 PM
RE: Sorting Steps - by Pedroski55 - Mar-29-2024, 09:10 PM
RE: Sorting Steps - by deanhystad - Mar-29-2024, 10:55 PM
RE: Sorting Steps - by MoreMoney - Mar-30-2024, 03:15 PM
RE: Sorting Steps - by Pedroski55 - Mar-30-2024, 07:54 AM
RE: Sorting Steps - by MoreMoney - Mar-30-2024, 03:10 PM
RE: Sorting Steps - by Pedroski55 - Mar-31-2024, 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Next steps for using API data Rebster 6 2,606 Oct-10-2020, 05:35 PM
Last Post: perfringo
  Keep Toggle View through Slider Steps yourboyjoe 1 1,705 Aug-10-2020, 07:32 PM
Last Post: Yoriz
  Files to store configuration and steps for a industrial process control application Danieru 1 1,827 Aug-03-2020, 05:43 PM
Last Post: buran
  Pexpect baby steps slouw 9 8,035 May-23-2019, 10:21 AM
Last Post: heiner55
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,135 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  first steps with python3 hunnimonstr 5 4,616 Jul-03-2017, 08:49 PM
Last Post: hunnimonstr

Forum Jump:

User Panel Messages

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