3

One of my assignments is to write a python program that sorts three numbers in ascending orders without use of any functions like sort or swap.

This is the first thing I came up with:

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

My teacher said to try and simplify it so I wrote this next (which basically is a roundabout way to use swap):

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

So I guess my question is whether it can be more simplified than this and what that would look like.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50

6 Answers6

4

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)
srishtigarg
  • 1,106
  • 10
  • 24
1

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)

Dovakiin
  • 21
  • 5
0

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.

Hillel
  • 811
  • 2
  • 7
  • 19
  • With your first code, with this input: `Enter a: 1.1e308 Enter b: 1e308 Enter c: 0`, I get `nan < nan < inf`. Don't use such bad tricks! – Thierry Lathuille Jan 04 '21 at 10:21
  • I was trying to preserve op's concept and focus on showing that moving the last IF inside the second one might improve performance - I agree that swapping numbers with op's method is naive – Hillel Jan 04 '21 at 10:33
  • You could edit your answer to include a big "Don't do that" with an example of how it could fail" - feel free to use this one - and how your second version is better, that would make your answer much more valuable. – Thierry Lathuille Jan 04 '21 at 10:36
0

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

#simple program to sort a list in ascending order

name = [1, 2, 0, 1, 5, 3, 0, 8, 2, 9]
len1=len(name)
for i in range(len1): #iteration from 1st value
   for j in range(i+1,len1): #iteration from i+1 
       if  name[i] < name[j]: #this is ascending order (*the < changed to > then ascending order)
           name[i],name[j]=name[j],name[i] #swapping logic in python
print (name)
manjunath
  • 37
  • 4
-1
a = int(input("Please enter the first value: "))
b = int(input("Please enter the second value: "))
c = int(input("Please enter the third value: "))
lst = []
lst.append(a)
lst.append(b)
lst.append(c)
print(sorted(lst))
Clau
  • 1
  • I would say that using `sorted` to sort a list would break the "without using functions" requirement of the question. – Henry Ecker May 23 '22 at 03:12