import java.util.Scanner;
public class SelectionSort {
public static int[] SelectionTest(int[] a){
for(int i = 0;i<a.length-1;i++){
int min = a[i];
int Index = 0;
for(int j=i+1;j<a.length;j++){
if(min>a[j]){
min= a[j];
Index= j;
}
}
a[Index]=a[i];
a[i]=min;
}
return a;
}
public static void main(String[] args){
Scanner input= new Scanner(System.in);
System.out.println("please enter in ...");
int [] array = new int [10];
for(int i = 0;i<array.length;i++){
array[i]=input.nextInt();
}
int[] a=SelectionTest(array);
for(int i = 0;i<array.length;i++){
System.out.println(array[i]);
}
}
}
2 回答
慕粉4067798
TA贡献1条经验 获得超0个赞
public static int[] SelectionTest(int[] a){
for(int i = 0;i<a.length-1;i++){
int min = a[i];
int Index = 0;//1
*
*
a[Index]=a[i];//2
a[i]=min;
错误在//1这里,如果这层循环没有比a[i]小的,那么最后就会让//2处的a[index]=a[i],
也就是a[0]=a[i]
}
添加回答
举报
0/150
提交
取消