3

Specs: Ubuntu 13.04, Python 3.3.1

Background: total beginner to Python, came across this "manual sorting" problem.

What I was asked to do: "Have the user enter 3 numeric values and store them in 3 different variables. Without using lists or sorting algorithms, manually sort these 3 numbers from smallest to largest."

What I was able to come up with:

number = input("Please enter 3 numbers: ")
number = list(number)

a = int(number[0])
b = int(number[1])
c = int(number[2])

new_l = []

if a > b and a > c:
    new_l.append(a)
    if b > c:
        new_l.append(b)
        new_l.append(c)
    else:
        new_l.append(c)
        new_l.append(b)
    print(new_l)

if b > a and b > c:
    new_l.append(b)
    if a > c:
        new_l.append(a)
        new_l.append(c)
    else:
        new_l.append(c)
        new_l.append(a)
    print(new_l)

if c > a and c > b:
    new_l.append(c)
    if a > b:
        new_l.append(a)
    else:
        new_l.append(b)
        new_l.append(a)
    print(new_l)

So my question is: I realize that my solution is extremely limited. First it can only process 3 single digit numbers since once the input string is converted into a list, there is no way to break all digits correctly into individual numbers the user intended. Second,by using this solution, the coder is forced to enumerates all possible scenarios for the 3 numbers to compare with each other, which could be very inflexible if say, the script were to be changed to accepting user input of 100+ numbers.

If you could share some guidance regarding the question above, or regarding how to solve this problem in a different way, I'll be very greatful! Thank you.

hakuna121
  • 243
  • 5
  • 10
  • 18
  • 2
    "*Without using lists or sorting algorithms*": So you want to sort something without sorting it? – Blender Jun 06 '13 at 16:13
  • The way I understood the question is that we are asked not to use .sort() method or sorted() function, but to sort the list of number manually? – hakuna121 Jun 06 '13 at 16:17
  • But it says that you can't use lists either, so how are you going to store the numbers? – Blender Jun 06 '13 at 16:18
  • Oh I missed the list part. Then my solution isn't right since I used list in it. Sharp eye! Thank you. – hakuna121 Jun 06 '13 at 18:41

4 Answers4

10

For three items, you could use max and min to sort them:

a, b, c = 3, 1, 8

x = min(a, b, c)  # Smallest of the three
z = max(a, b, c)  # Largest of the three
y = (a + b + c) - (x + z)  # Since you have two of the three, you can solve for
                           # the third

print(a, b, c)
print(x, y, z)

If you don't want to use a sorting algorithm but can use lists, you could just pop out the smallest item each time and store it in a new list:

numbers = [1, 8, 9, 6, 2, 3, 1, 4, 5]
output = []

while numbers:
    smallest = min(numbers)
    index = numbers.index(smallest)
    output.append(numbers.pop(index))

print(output)

It's pretty inefficient, but it works.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thank you for answering! I'm particularly glad to learn from your answer about the excellent approach to the y variable. Btw, would you care to elaborate why the print(a,b,c) part? Thank you! – hakuna121 Jun 06 '13 at 23:53
  • @hakuna121: It's just printing out the unsorted and the sorted values. – Blender Jun 06 '13 at 23:56
1

Using the Bubble Sort Algorithm:

num1=input("Enter a number: ")
num2=input("Enter another number: ")
num3=input("One more! ")
if num1<num2:
    temp=0
    temp=num1
    num1=num2
    num2=temp
if num1<num3:
    temp=0
    temp=num1
    num1=num3
    num3=temp
if num2<num3:
    temp=0
    temp=num2
    num2=num3
    num3=temp
print num3, num2, num1
Neuron
  • 5,141
  • 5
  • 38
  • 59
1
def smallest(li):
    small=li[0]
    index=0
    for i in range(1,len(li)):
        if small>li[i]:
            small=li[i]
            index=i
    return small,index
def sort_list(li): #li is the list to be sorted
    for  i in range(len(li)):
        val,index=smallest(li[i:])
        li.pop(index+i)
        li.insert(i,val)

sort_list(li) #passing the list to the function
    

Alright what we are actually doing here is

First we have created a function "smallest()" which returns the smallest value and and the index of that value in the list that passed to it.

Then we have created a another fuction where we sort the list "sort_list".

so, what we are doing in that function is passing the list to the smallest() function and by using the index of the smallest val we first pop it from the list then insert it with the value 'i' which is of in the loop range(len(list)) well we use that i to slice the list which we pass to the fuction smallest() also add it with the returned index to pop the right value from the non-sliced list.

when this is process is done for len(list) times your list will get sorted.

time complexity: O(n*(n/2)) which is O(n^2)

0

For Sorting a list manually, you can implement any kind of sorting algorithm like bubble sort, selection sort, insertion sort etc. so you can try the following code of bubble sort

#Bubble sort in python
def bubbleSort(numbers):
  for i in range(len(numbers)):
    for j in range(len(numbers)-i-1):
      if(numbers[j]>numbers[j+1]):
        temp=numbers[j]
        numbers[j]=numbers[j+1]
        numbers[j+1]=temp
        
#taking space seperated numbers as input in list
numbers=list(map(int, input().split(' ')));
bubbleSort(numbers)
print(numbers)
Ruman
  • 936
  • 11
  • 9