Challenge completed!

--Originally published at Hector Martinez Alcantara

Here’s my code of the second challenge from the class TC101.

It consist in sorting some numbers, an user type some numbers separated by coma in the terminal, and then the program do the bubble sort to re-order the numbers from smallest to largest.

First I read the numbers, then I compare two positions, to check if the number is higher than the previous number and if it’s the case, they change positions, doing this iteration length – 1 times.

That’s called bubble sort.

text=input("Type some numbers separated by coma\n")
numbers=text.split(',')
print("In disorder")
for number in numbers:
 print(number)
length=len(numbers)-1
for iteration in range(0,(length)):
 for position in range(0,(length)):
 if numbers[position] > numbers[position+1]:
 change=numbers[position]
 numbers[position]=numbers[position+1]
 numbers[position+1]=change
print("In order")
for number in numbers:
 print(number)

Thanks for reading.