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
#1
Hello, I'm currently taking a python class, I really enjoy it but I'm having a issue with getting my code for a upcoming assignment to work. The teacher wants us to sort 3 numbers in acceding order without using the sort function built into python. Also the user can put in the numbers in any order. So in terms, no matter what 3 numbers the user puts in the program will compare using if statements and find the smallest number, the middle number and the larger number. Here's the code I've wrote, I keep getting 0, 0, 0 in my return will not sort. As you can see, when the user types in a number on the first number it will compare to the second and third to see which number is smaller or larger. So if they typed in first, 5, second, 20, third, 3 it would list 3, 5, 20 in ascending order. I hope I explained this right...

#Get user input
first = int(input("Enter the first number: "))
second = int(input("Enter the second number: "))
third = int(input("Enter the third number: "))

small = 0
middle = 0
large = 0

# IF Statement

#SMALL
if first < third and first < second:
    small = first
    if second < third and second < first:
        small = second
    else:
        small = third

#MIDDLE
elif first < second and first < third:
    middle = first
    if second > first and second < third:
        middle = second
    else:
        middle = third

#LARGE
elif first > second and first > third:
    large = first
    if second > first and second > third:
        large = second
    else:
        large = third



# Display Results
print("The numbers in accending order are: ", small, middle, large)
Thanks for anyone who helps out!
Reply
#2
Your indentation and use of elif is off. Take your second if statement on line 15. Since it is indented under the first if statement on line 13, it is only checked if the condition on line 13 is True. But then, the condition on line 15 must be False, so it never executes. You want to unindent lines 15-18, and change line 15 to an elif. Then it line 15 will only be checked if line 13 is False, in which case line 15 has a chance of being True.

Next is your elif on line 21. It only executes if the condition on line 13 is False. But you always want to check for what the middle number is. It shouldn't depend on line 13. So make that an if instead of an elif.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The line numbers are you talking about up there in the code I posted or in python itself? Also is my if statement comparisons right? I’m kind of second guessing if I have it comparing correctly. Since it’s 3 if statements per input of line.
Reply
#4
(Oct-13-2017, 03:01 AM)dtweaponx Wrote: The line numbers are you talking about up there in the code I posted or in python itself? Also is my if statement comparisons right? I’m kind of second guessing if I have it comparing correctly. Since it’s 3 if statements per input of line.
ichabod801 will be referring to the line numbers as shown in the post you made - we have no idea what line numbers might apply in the editor you are using elsewhere (but would expect them to be the same if you have pasted like-for-like).

I'm also struck that the assignment was to sort three numbers into ascending order, not to determine the lowest, middle and highest. You might want to do a google search for sort algorithms (look at bubble sort and quick sort).

I note you are checking for some redundant conditions. If you've already established that first is smaller than anything else, then you have no need to check within the block where that condition applies to see if the second is smaller than the first.

Incidentally, what happens if the user enters some numbers that are the same?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
can you use min() function?
Reply
#6
(Oct-13-2017, 08:18 AM)gruntfutuk Wrote:
(Oct-13-2017, 03:01 AM)dtweaponx Wrote: The line numbers are you talking about up there in the code I posted or in python itself? Also is my if statement comparisons right? I’m kind of second guessing if I have it comparing correctly. Since it’s 3 if statements per input of line.
ichabod801 will be referring to the line numbers as shown in the post you made - we have no idea what line numbers might apply in the editor you are using elsewhere (but would expect them to be the same if you have pasted like-for-like).

I'm also struck that the assignment was to sort three numbers into ascending order, not to determine the lowest, middle and highest. You might want to do a google search for sort algorithms (look at bubble sort and quick sort).

I note you are checking for some redundant conditions. If you've already established that first is smaller than anything else, then you have no need to check within the block where that condition applies to see if the second is smaller than the first.

Incidentally, what happens if the user enters some numbers that are the same?

I wish we could use quick sort, but the teacher said no, we have to develop our own sort system by seeing which number is the smallest in the 3 numbers the user typed in, it could be any order the user typed in like 50, 2 1 or 4, 30, 1 or 4, 1, 30. So the code would find the smallest and assign the smallest number to the small variable after it checked all 3 inputs, than it would move onto the next one to find say the largest. and then I'd guess to find the middle one would simply a if statement like if middle is larger than smallest but less than largest it would be middle?
Reply
#7
(Oct-13-2017, 11:06 AM)dtweaponx Wrote: I wish we could use quick sort, but the teacher said no, we have to develop our own sort system by seeing which number is the smallest in the 3 numbers the user typed in, it could be any order the user typed in like 50, 2 1 or 4, 30, 1 or 4, 1, 30. So the code would find the smallest and assign the smallest number to the small variable after it checked all 3 inputs, than it would move onto the next one to find say the largest. and then I'd guess to find the middle one would simply a if statement like if middle is larger than smallest but less than largest it would be middle?

Wow! How frustrating. Well, have fun, and don't forget to deal with the unusual inputs (unless the teacher promised 'happy-path' only. Smile )
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
First would make a list of the numbers.
#Get user input
numb_list = []
numbers = input("Enter three numbers separated by space: ")
numb_list = [int(i) for i in numbers.split()]
sort_list = []
So could take the the first number in numb_list and compare if number is less(<) than other number in number list.
Then make a temporary variable,and append that number to sort_list and remove from numb_list.
All this is done as long as there are numbers in numb_list.
It's hard to explain this stuff Cry  

The start look like this:
while numb_list:
    first_number = numb_list[0]
    for n in numb_list:
        if n < first_number:
Do small test to understand it:
>>> lst = [50, 2, 1]
>>> for n in lst:
...     if n < 50:
...         print(n)
...         x = n
... 
2
1
>>> x
1

# x will always be the lowest number
>>> lst = [5, 100, 789, 4, 5678, 1, 444]
>>> for n in lst:
...     if n < 5:
...         print(n)
...         x = n
... 
4
1
>>> x
1
Reply
#9
Given the restrictions the teacher has placed on it, I'd go with something along these lines:

def threesort(three):
    if three[0] <= three[1]:
        if three[1] <= three[2]:
            return
        else:
            threeswap(three, 1)
    else:
        threeswap(three, 0)
    threesort(three)
This function assumes:
  • you are passing a list of three numbers
  • the function threeswap swaps the list values at the indexed and indexed + 1 positions
I am trying to help you, really, even if it doesn't always seem that way
Reply
#10
(Oct-13-2017, 03:46 PM)gruntfutuk Wrote: Given the restrictions the teacher has placed on it, I'd go with something along these lines:

def threesort(three):
    if three[0] <= three[1]:
        if three[1] <= three[2]:
            return
        else:
            threeswap(three, 1)
    else:
        threeswap(three, 0)
    threesort(three)
This function assumes:
  • you are passing a list of three numbers
  • the function threeswap swaps the list values at the indexed and indexed + 1 positions

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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog codility leap sort variant MoreMoney 5 357 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,355 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,397 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,197 Apr-24-2020, 01:29 PM
Last Post: Godserena
  Sort objects by protected properties. Sigriddenfeta 1 1,876 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