Python Forum

Full Version: [split] Manual Sort without Sort function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#Get user input
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))
 
small = 0
middle = 0
large = 0
 
# IF Statement
#SMALL
if a < b and a < c:
    small = a
elif b < a and b < c:
    small = b
else:
    small = c

 
#MIDDLE
if (a < b  and  b < c) or ( c < b  and  b < a):
   middle = b
elif( b < a  and a < c )or (c < a  and a < b):
    middle = a
elif (a < c  and c < b) or (b < c  and c < a)we:
    middle = c

    
#LARGE
if a > b and a > c:
    large = a
elif b > a  and b > c:
    large = b
else:
    large = c
 
 
 
# Display Results
print("The numbers in accending order are: ", large, middle, small)
I've split your post into a separate thread so as to not cause notifications on a ~2 year old thread. With that in mind, were you trying to complete the OP's homework, or were you trying to ask a question?
Nice Sunday morning wakeup exercise for the brain Smile

In my mind there is only three comparisons needed to order three numbers: compare two numbers and find their order; then find third numbers position (it can be either before first, before second or at the end) by comparing if it's smaller than first or second number, if not then it's largest.

>>> a = 5
>>> b = 10
>>> c = 2
>>> lst = []
>>> if a < b:
...     lst.extend([a, b])
... else:
...     lst.extend([b, a])
...
>>> lst
[5, 10]
>>> if c < lst[0]:
...     lst.insert(0, c)
... elif c < lst[1]:
...     lst.insert(1, c)
... else:
...     lst.append(c)
...
>>> lst
[2, 5, 10]