冒泡排序

来源:互联网 发布:springmvc json 编辑:程序博客网 时间:2024/06/07 18:42
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("");
}

}



打印结果:

67 100 89 32 564377
67 89 1003256 43 77
32 67 8910056 43 77
32 56 6789100 43 77
32 43 566789 100 77
32 43 566777 89 100



理解:内循环每完整遍历一次   都讲数组中最大的数放在【i】索引位置上,且i位之前的数一定是按大小顺序排列的


原创粉丝点击