Python Forum
[split] Manual Sort without Sort function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: [split] Manual Sort without Sort function (/thread-18809.html)



[split] Manual Sort without Sort function - fulir16 - Jun-01-2019

#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)



RE: [split] Manual Sort without Sort function - micseydel - Jun-01-2019

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?


RE: [split] Manual Sort without Sort function - perfringo - Jun-02-2019

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]