defselection_sort(collection):
length =len(collection)for i inrange(length-1):
least = i;for k inrange(i+1,length):if collection[k]< collection[least]:
least = k
if least != i:
collection[least],collection[i]=(collection[i],collection[least])return collection
if __name__ =="__main__":
user_input =input("输入排序数字,按逗号分隔:\n").strip()
unsorted =[int(item)for item in user_input.split(",")]print(selection_sort(unsorted))
java实现:
publicclassselect_sort{publicstaticvoidmain(String[] args)throws IOException {
System.out.println("输入排序数字,按逗号分隔:");
BufferedReader br =newBufferedReader(newInputStreamReader(System.in));
String[] arra = br.readLine().split(",");
Integer[] array =newInteger[arra.length];for(int i=0; i<arra.length; i++)
array[i]= Integer.parseInt(arra[i]);for(int i=0;i<selectSort(array).length;i++)
System.out.print(array[i]+",");}privatestatic Integer[]selectSort(Integer[] notSort){int len = notSort.length;int least =0;for(int i =0; i<len-1;i++){
least = i;for(int j = i+1; j < len ; j++){if(notSort[least]> notSort[j])
least = j;}if(least != i){int temp =0;
temp = notSort[least];
notSort[least]= notSort[i];
notSort[i]= temp;}}return notSort;}}