你好,游客 登录 注册 搜索
背景:
阅读新闻

关于冒泡算法实现

[日期:2014-08-31] 来源:Linux社区  作者:spring87 [字体:大 中 小]

首先冒泡算法就是每次把最大的找出来,冒泡出去,但是有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] + "\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();

        }

    }

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/106008.htm

linux
相关资讯       冒泡算法 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款