设为首页 加入收藏

TOP

关于冒泡算法实现
2014-11-23 21:31:44 来源: 作者: 【 】 浏览:7
Tags:关于 冒泡 算法 实现

首先冒泡算法就是每次把最大的找出来,冒泡出去,但是有2种不同实现。


第一:


public class Test12{


public static void main(String[] args){/*


int score[] = {67, 88, 45, 87, 29, 99, 109, 100};


for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序


for(int j = 0 ;j < score.length - i - 1; j++){ //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)


if(score[j] > score[j + 1]){ //把小的值交换到后面


int temp = score[j];


score[j] = score[j + 1];


score[j + 1] = temp;


}


}


System.out.print("第" + (i + 1) + "次排序结果:");


for(int a = 0; a < score.length; a++){


System.out.print(score[a] + "\t");


}


System.out.println("");


}


System.out.print("最终排序结果 :");


for(int a = 0; a < score.length; a++){


System.out.print(score[a] + "\t");


}


*/


int[] score = {67, 88, 45, 87, 29, 99, 109, 100};


bubblesort(score);


}



第二种:


//===============冒泡排序---之最=====================================================


public static void bubblesort(int[] arry){


for(int i = 0;i < arry.length-1;i++){


for(int j = arry.length-1;j > i;j--){


if(arry[j]>arry[j-1]){


int temp = arry[j];


arry[j] = arry[j-1];


arry[j-1] = temp;


}


}


System.out.print("第"+(i+1)+"次排序:");


for (int i1 : arry) {


System.out.print(i1 +" ");


}


System.out.println();


}


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇AngularJS 路由的安全性处理 下一篇TQ2440外接GPIO蜂鸣器驱动程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: