0
Currently I have studied a little Python and, amid the studies I am trying to perform a challenge where I need to implement a Bubble Sort algorithm in Python. The criteria are:
- The algorithm receives a string as input and sorts the given values.
- This string must be separated by blanks
- Input: integra 10 8 2 3 5 1' Output: pra 1 2 3 5 8 10'
I implemented the algorithm below, but what I have received as output is: ['1', '10', '2', '3', '5', '8']
I can’t see the error in my logic, if anyone can help me how to solve this and point out where my mistake is.
def sort(array): for final in range(len(array), 0, -1): exchanging = False for current in range(0, final - 1): if array[current] > array[current + 1]: array[current + 1], array[current] = array[current], array[current + 1] exchanging = True if not exchanging: break array = sorted(['10', '8', '2', '3', '5', '1']) # entrada: 10 8 2 3 5 1 saída: ‘1 2 3 5 8 10’ sort(array) print(array)