登 录
冒泡排序:对数组进行遍历,每次遍历都是为了找出最大值,并将最大值放在最前面。
代码如下:
//冒泡排序 /*i 的每一次循环都会找出一个从score[i]往后的数中的最大值,并将它放在前面*/ public class ArrayDemo02 { public static void main(String args[]){ int score[]={11,3,55,67,45,23,56,89,655,2,4,5,7,9,4,1,5,7,6,4,5,3,4,6,7,22}; for(int i=0;i<score.length;i++){ for(int j=i+1;j<score.length;j++){ if(score[i]<score[j]){ //如果score[i]<score[j],则二者进行交换,小的到了后面,大的到了前面 int temp=score[i]; score[i]=score[j]; score[j]=temp; } } } for(int i=0;i<score.length;i++){ //输出排序后的数组 System.out.print(score[i]+"/t"); } } }
抱歉!评论已关闭.