Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 关于冒泡算法实现

首先冒泡算法就是每次把最大的找出来,冒泡出去,但是有2种不同实现。C# 冒泡算法 http://www.linuxidc.com/Linux/2012-06/63178.htm第一: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] + " ");            }            System.out.println("");        }            System.out.print("最终排序结果 :");            for(int a = 0; a < score.length; a++){                System.out.print(score[a] + " ");     }    */        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();        }    }本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/106008.htm