# Sorting 3 numbers

The goal of my program is the sort three numbers that the user enters in. I thought about writing if, elif, and else statements, but I feel like this is too tedious and not “professional”. Here’s a portion of my program, I thought of

def sort_numbers(num1, num2, num3):
greatest = max(num1, num2, num3)
least = min(num1, num2, num3)
middle = (num1 + num2 + num3) - least - greatest
print(least, middle, greatest)

Now when try inputing 3, 2.4, 5 into the program. It sorts it correctly, but instead of 2.4, 3, 5. I got 2.4, 3.0, 5. I would like the numbers to appear as they were inputted (3 instead of 3.0). Is there any way to overcome this in my snippet of code. Or is there a more efficient method like doing if, elif, else statements, … ?

Thanks.

``````>>> def sort_three(*args):
if len(args) != 3: return False
a, b, c = args
if a < b < c:
pass
else:
if a > c: a, c = c, a
if a > b: a, b = b, a
if b > c: b, c = c, b
return a, b, c

>>> sort_three(1, 2, 3)
(1, 2, 3)
>>> sort_three(3, 2, 1)
(1, 2, 3)
>>>
``````

Three (possible) steps to sort three numbers.

Will it take four steps to sort four? Not very likely. More like six. It goes up sequentialy as we increase the number of items in the list. If we examine the steps taken for three, we may deduce a bubble sort algorithm from them, which for less than a hundred, or even a few hundred items, is quick enough to be an acceptable method.

An interviewer will be looking for this sort of thinking in one’s approach to a sorting algorithm (know the size of the data).

``````>>> def sort_four(*args):
if len(args) != 4: return False
a, b, c, d = args
if a < b < c < d:
pass
else:
if a > d: a, d = d, a
if a > c: a, c = c, a
if a > b: a, b = b, a
if b > d: b, d = d, b
if b > c: b, c = c, b
if c > d: c, d = d, c
return a, b, c, d

>>> sort_four(4,3,2,1)
(1, 2, 3, 4)
>>> sort_four(1,2,3,4)
(1, 2, 3, 4)
>>> sort_four(2, 3, 4, 1)
(1, 2, 3, 4)
>>>
``````
``````>>> sort_four('four', 'fabulous', 'flying', 'falcons')
('fabulous', 'falcons', 'flying', 'four')
>>>
``````

``````>>> def sort_five(*args):
if len(args) != 5: return False
a, b, c, d, e = args
if a < b < c < d < e:
pass
else:
if a > e: a, e = e, a
if a > d: a, d = d, a
if a > c: a, c = c, a
if a > b: a, b = b, a
if b > e: b, e = e, b
if b > d: b, d = d, b
if b > c: b, c = c, b
if c > e: c, e = e, c
if c > d: c, d = d, c
if d > e: d, e = e, d
return a, b, c, d, e

>>> sort_five(1,2,3,4,5)
(1, 2, 3, 4, 5)
>>> sort_five(5,4,3,2,1)
(1, 2, 3, 4, 5)
>>>
``````

0, 1, 3, 6, 10, 15, 21, 28, 36, 47, 57, and so on…

That’s the steps it takes to sort n up to 11 items, and so on. Predictible and therefore a crossover point may be determined for when this method becomes unfeasible.

1 Like

Extended example of Bubble Sort

``````def bubble_sort(sample):
for i in range(len(sample)):
for j in range(len(sample) - 1, i, -1):
if sample[i] > sample[j]:
sample[i], sample[j] = sample[j], sample[i]
return sample
``````

https://repl.it/@mtf/Basic-Bubble-Sort

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.