Sorting three numbers in ascending order without using functions

We Are Going To Discuss About Sorting three numbers in ascending order without using functions. So lets Start this Python Article.

Sorting three numbers in ascending order without using functions

  1. How to solve Sorting three numbers in ascending order without using functions

    I think using your approach in the second code, this is the easiest way out:
    a = float(input("Enter a: ")) b = float(input("Enter b: ")) c = float(input("Enter c: ")) if a > b: a,b = b,a if a > c: a,c = c,a if b > c: b,c = c,b print (a, "<", b, "<", c)

  2. Sorting three numbers in ascending order without using functions

    I think using your approach in the second code, this is the easiest way out:
    a = float(input("Enter a: ")) b = float(input("Enter b: ")) c = float(input("Enter c: ")) if a > b: a,b = b,a if a > c: a,c = c,a if b > c: b,c = c,b print (a, "<", b, "<", c)

Solution 1

I think using your approach in the second code, this is the easiest way out:

a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))

if a > b:
    a,b = b,a
if a > c:
    a,c = c,a
if b > c:
    b,c = c,b
    
print (a, "<", b, "<", c)

Original Author srishtigarg Of This Content

Solution 2

not sure if this is what you want but you cud use list and sort them like this:

for i in range(len(num)):
    for j in range(i+1, len(num)):
        if(num[i] > num[j]):
            temp = num[i]
            num[i] = num[j]
            num[j] = temp
print(num)

Original Author Dovakiin Of This Content

Solution 3

Not simple but shorter…

a=[int(input("First Number: ")),int(input("Second Number: ")),int(input("Third Number: "))]
for x in range (0,len(a)):
    for i in range (0,(len(a))-1):
        if a[i]>a[i+1]:
            a[i],a[i+1] = a[i+1],a[i]
print ("Little to Bigger Sort:",a)

Original Author Emre İpşir Of This Content

Solution 4

Preserving your concept, I would go with something like this (Don’t use this version because your swapping concept still needs fixing – see comments below by @thierry-lathuille):

a = float(input("Enter a: "))
b = float(input("Enter b: "))
if a > b:
    a = a + b
    b = a - b
    a = a - b
c = float(input("Enter c: "))
if b > c:
    b = b + c
    c = b - c
    b = b - c
    if a > b:
        a = a + b
        b = a - b
        a = a - b
print (a, "<", b, "<", c)

This way most scenarios will lead to just two if statements instead of three

Combined with the correct a, b = b, a swapping notation recommended by @khelwood we get:

a = float(input("Enter a: "))
b = float(input("Enter b: "))
if a > b:
    a, b = b, a
c = float(input("Enter c: "))
if b > c:
    b, c = c, b
    if a > b:
        a, b = b, a
print (a, "<", b, "<", c)

This has the advantage of sometimes using only two IF statements instead of three, repairing the faults on the swap process, and having a simplified and more readable code.

Original Author Hillel Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment