简体   繁体   English

如何进行选择排序?

[英]How do I do selection sort?

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?这就是我启动代码时所拥有的全部内容,它只显示一位数字,但是当我输入 12 时,我得到 '1' '2' 如何让它保持 12 并对数字从小到大排序?

input("Please enter your numbers: ") - this returns string input("Please enter your numbers: ") - 这返回字符串

list(string) - will separate string on every character and make list of them list(string) - 将在每个字符上分隔字符串并列出它们

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.我使用了split() function,当我说string.split(' ')时,它会在每个' '上分隔字符串并返回列表。 For sorting the list you can use the built-in function sort() , or you can make your own.要对列表进行排序,您可以使用内置的 function sort() ,或者您可以自己制作。 This page will help you to make your sorting function with selection sort .此页面将帮助您使用选择排序进行排序 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM