Home > Back-end >  How do I do selection sort?
How do I do selection sort?

Time:05-18

numbers = list (input("Please enter your numbers: "))

print (numbers)

This is all I have when I start the code it only shows single digit numbers but when I put 12, I get '1' '2' how do I make it stay 12 and sort the numbers smallest to biggest?

CodePudding user response:

input("Please enter your numbers: ") - this returns string

list(string) - will separate string on every character and make list of them

I think this will do what you want:

numbers = [int(x) for x in input("Please enter your numbers: ").split(' ')]
print (numbers)

I used split() function, and when i say string.split(' ') it will separate string on every ' ' and return the list. For sorting the list you can use the built-in function sort(), or you can make your own. This page will help you to make your sorting function with selection sort.

CodePudding user response:

Use numbers.sort() to sort the list of integers

  • Related