public class Bubble {
public static void main(String[] args) {
// TODO Auto-generated method stub
int start[] = { 10 , 23 , 11 , 56 , 45 , 26 , 59 , 28 , 84 , 79 };
int i,temp;
for(i = 0; i < start.length; i++){
System.out.print(start[i] + " ");
}
System.out.println();
for(i = 0; i < start.length - 1; i++){
if(start[i] > start[i+1]){
temp = start[i];
start[i] = start[i+1];
start[i+1] = temp;
}
}
for(i = 0; i < start.length; i++){
System.out.print(start[i] + " ");
}
}
} 请大神指出问题所在,谢谢!
2 回答
已采纳
混沌木头
TA贡献3条经验 获得超3个赞
public class Bubble { public static void main(String[] args) { int start[] = { 10, 23, 11, 56, 45, 26, 59, 28, 84, 79 }; for(int i = 0;i<start.length-1;i++){//外层循环控制要循环的轮数 for(int j = 0;j<start.length-1-i;j++){//内层循环控制每轮比较的次数 if( start[j]>start[j+1]){//交换 int temp = start[j]; start[j] = start[j+1]; start[j+1] = temp; } } } System.out.println(Arrays.toString(start)); } }
少了一层循环
添加回答
举报
0/150
提交
取消