Python Forum
Manual Sort without Sort function
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manual Sort without Sort function
#11
(Oct-13-2017, 04:55 PM)dtweaponx Wrote: Man we haven’t even dove that deep into python yet. I’m very interested in python but this assignment is giving me a headache with how strict the rules to it are. Practically the teacher only wants us to use if, elif and else statements and >, <, comparisons for this project. I’m getting really confused on the if statements. Like a single if statement is going to check for true or false? Than another if below it will check another condition then elif would assign?
Ok, baby steps.

The problem most people have when they start programming is that they cannot believe how dumb computers really are. Humans make lots of intuitive jumps and use learned-long-ago behaviours and we find it very hard at first to lay out each little step a computer must do.

Consider for example living in a block of flats, with a set of open mailboxes in the lobby. You realise that the mailman has been putting post for flat 6 in box 9 and flat 9 in box 6. You decide to swap the post over, but you see that the post (one mailbox sized heavy box in each) entirely fills each box and you cannot hold one while you move the other.

Instinctively, as a human, you would grab one and stick it into a spare box temporarily (or on the floor, or on top of the radiator, or ... something else), then move the other, then move the first from its temporary location to correct mailbox.

You can't just tell a computer to swap things over. You have to write detailed instructions.

With the if statement, yes you can only have two possible outcomes. Either a condition is True or it is False. The evaluation of the condition can be very complex involving lots of considerations.

Thinking about going out tonight:

if it is not raining and I have money in my wallet and there is nothing good on the tv then I can go out.

As you know, you can break this down into multiple conditions. Maybe you are not worried about the rain, but you need to check you have the rain coat with you rather than having left it at a friends.

When you start out programming, try to think about things in the real world. These things will be pretty dumb. For this problem, there is nothing wrong with putting three placemats on the table in front of you, writing some numbers down on small squares of paper, and randomly placing three of them into the placemats.

Now, rather than looking at the computer screen and getting frustrated, walk through how to tell someone that is really really stupid how to sort those three numbers into the right order. Use very simple, basic instructions. (You can assume they know how to pick something up and how to put it down, but you can't let them hold anything whilst anything else happens. You can't just point or be vague either. Saying "pick the next one up" or "put it in a spare slot" will not work because you are asking them to figure something out, and that part is the programmer's job.)

I know this sounds really dumb, but trust me, make it real, make it simple, do it and the light will dawn, and soon you will not need to do things so basically.

Regarding the code I gave you,

def threesort(three):
    if three[0] <= three[1]:
        if three[1] <= three[2]:
            return
        else:
            threeswap(three, 1)
    else:
        threeswap(three, 0)
    threesort(three)
Apologies for posting something ahead of your current learning stage.

Let me break it down for you.

def defines a block of code. I'm sure you've had to sing music at school at some point, and on the song sheet, there will have been a section marked as the chorus, but rather than having it printed every time it is needed between verses, it will be printed only the first time and after that they will have just printed "chorus" and you know to sing the chorus printed earlier on in the song sheet.

In code,
  • easy to do the same thing in more than one place in your main code
  • you only need to change the code that does that thing in one place
  • makes it easy to understand the main code, because fiddly detail is written elsewhere

A defined block can be passed arguments to work with. The singing version might be to indicate a chorus should be sung faster, louder, softer (or maybe the name of a person mentioned in the chorus should be different each time it is sung).

Instead of having a def block, you can simply have a loop in your main code, to execute the same block of code as many times as required. You just have to figure out how to control the loop.

OR, with something very simple, that you know will need to happen only a few times at most, well, you can repeat the code. Thanks to copy and paste, it is not hard.

The odd part of the code I shared is probably the last line of the block where it calls itself. This is called recursion. If it happens too many times, you can crash the computer because all of the memory gets used up. In this case, because we are dealing with only three numbers, I knew that would not happen. If I didn't have that line, after the code has been executed once, it would not happen again UNLESS the part of the main code that was calling it was doing so in a loop. Alternatively, I could build a loop into that def block so it executed the required number of times to solve the problem. We would be back to that thing about controlling the loop I mentioned before.

The recursion calls makes sure that block of code keeps being called until the job is done. How do we know the job is done? When both if statements confirm things are in the right order, in which case the return command is used to exit the block of code. In the case of recursion, this will back out much like packing a set of Russian dolls.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#12
Each if statement has a condition. If the condition evaluates as True, the indented code under the if statement executes. If the condition is False, Python looks for an elif statement at the same indentation as the if statement (it has to be right after the indented code for the if statement). Each elif also has a condition, which if it is True, executes the indented block under the elif. When Python can't find any more elifs to check (because they've all been False), it looks for an else statement. If it finds one, it executes the indented code there. Remember, elif and else are only checked if all of the ifs and elifs before them are False. Each if starts a new set of if/elif/else, ignoring any previous conditions.

if a:
    # executed if a is True
elif b:
    # executed if a is False and b is True
elif c:
    # executed if a is False, b is False, and c is True
else:
    # executed if a, b, and c are all False.

if b:
    # executed if b is True, igoring a and c.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
Well, there was a similar topic a week ago or a bit more. Basically, what can you do is to get ( pop(list.index(min(list))) )the lowest value from the list and put it in a new list. That way you will 'sort' the new one removing every min value from the original list.

Baby steps:
  1. find the min value in the list
  2. get its index
  3. pop that index
  4. append the poped value to the new list
  5. fin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
Wavic, you do understand that that is a horribly inefficient way to sort a list, right (and I don't mean from a python perspective)?  If I asked you to sort 10,000 numbers using the above method without using python's built in sorting functions it would take quite some time.  I mean, in terms of doing 3 values he can (and should) just use a few conditionals.  Beyond that though one should implement an actual sorting algorithm (quicksort or mergesort probably)
Reply
#15
I do. But this is a homework after all.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#16
Regardless, it really isn't how you teach someone to sort.  Even a bubble sort (which I don't really think is a good way to teach sorting other than as a "what not to do") would be better for at least understanding the methodology.
Reply
#17
Hey everyone I seemed to have figured it out....sort of. Having a issue with getting the middle term. If you plug in numbers like 40 70 20 if returns 20 , 20 , 70.

BUT: if you plug them in 10, 40, 30. It will list them 10, 30, 40.


I've stared at this code for awhile, still a little confusing but not 100 percent. Here is my IF statement.

#SMALL
if first < third and first < second:
    small = first
elif second < third and second < first:
    small = second
else:
    small = third
 
#MIDDLE
if first < second and first < third:
    middle = first
if second > first and second < third:
    middle = second
if small < second and second < third:
    middle = second
else:
    middle = third
 
#LARGE
if first > second and first > third:
    large = first
elif second > first and second > third:
    large = second
else:
    large = third
 
All i'm trying to do is after it finds the smallest term, and the largest term it will compare between those 2 and assign the middle term? or am I overthinking this?
Reply
#18
I would look at your middle section. The first if condition is not correct.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#19
(Oct-14-2017, 02:02 AM)ichabod801 Wrote: I would look at your middle section. The first if condition is not correct.

I think I figured it out???? Just need someone with a little bit more experience to confirm. Here is my code.
Reply
#20
The middle is just like the small. Is the first number in between the other two? Then it's the middle. Is the second number between the other two? then it's the middle.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog codility leap sort variant MoreMoney 5 360 Apr-06-2024, 08:47 PM
Last Post: deanhystad
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,453 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,356 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sort and merge flintstone 1 1,896 Jun-14-2021, 07:32 PM
Last Post: Larz60+
  How can I sort my column so that NaN is at the bottom? soft 3 2,398 Feb-06-2021, 01:02 PM
Last Post: ibreeden
  how to sort a list without .sort() function letmecode 3 3,419 Dec-28-2020, 11:21 PM
Last Post: perfringo
  Sort on basis of (name,age,score) pyzyx3qwerty 9 9,527 May-14-2020, 08:29 AM
Last Post: pyzyx3qwerty
  Sort last pyzyx3qwerty 7 4,737 May-05-2020, 02:58 PM
Last Post: DeaD_EyE
  insertion sort brobro 3 2,198 Apr-24-2020, 01:29 PM
Last Post: Godserena
  Sort objects by protected properties. Sigriddenfeta 1 1,881 Mar-17-2020, 04:11 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