public static void main(String []args){
int []scores = {67,89,100,32,56,43,77};
for (int i=1;i<scores.length;i++) {
for (int j=0;j<scores.length;j++) {
if(scores[i]<scores[j]){
int temp = scores[i];
scores[i]=scores[j];
scores[j]=temp;
}
}
for (int l : scores) {
System.out.print(l+"\t");
}
System.out.println("");
}
int []scores = {67,89,100,32,56,43,77};
for (int i=1;i<scores.length;i++) {
for (int j=0;j<scores.length;j++) {
if(scores[i]<scores[j]){
int temp = scores[i];
scores[i]=scores[j];
scores[j]=temp;
}
}
for (int l : scores) {
System.out.print(l+"\t");
}
System.out.println("");
}
}
打印结果:
67 100
89 32 5643
77
67 89 10032
56 43
77
32 67 89100
56 43
77
32 56 6789
100 43
77
32 43 5667
89 100
77
32 43 5667
77 89
100
理解:内循环每完整遍历一次 都讲数组中最大的数放在【i】索引位置上,且i位之前的数一定是按大小顺序排列的