zoukankan      html  css  js  c++  java
  • JAVA 学习笔记 【冒泡算法】

    public class Bubble { 

    // 冒泡排序函数1 
    public static void bubbleSort1(Comparable []data){ 
       
      int position,scan; 
      Comparable temp; 
      for(position = data.length-1;position>=0;position--){ 
          for(scan=0;scan<=position-1;scan++){ 
        if(data[scan].compareTo(data[scan+1])<0){ 
            temp = data[scan]; 
            data[scan] = data[scan+1]; 
            data[scan+1]=temp; 
        } 
          } 
      } 
        } 
    // 冒泡排序函数2 
      public static int[] bubbleSort2(int[] m){ 
        
        int intLenth = m.length; 
        /*执行intLenth次*/ 
        for (int i=0;i<intLenth;i++){ 
            /*每执行一次,将最小的数排在后面*/ 
            for (int j=0; j<intLenth-i-1;j++) 
            { 
                int a = m[j]; 
                int b = m[j + 1]; 
                if (a < b) 
                { 
                    m[j] = b; 
                    m[j + 1] = a; 
                } 
            } 
        } 
        return m; 
      } 
       
       public static void main(String []args){ 
      
        // 冒泡排序1 
        Comparable []c={4,9,23,1,45,27,5,2}; 
        bubbleSort1(c); 
        for(int i=0;i<c.length;i++) 
            System.out.println("冒泡排序1:"+c[i]); 
         
         System.out.println("*******************"); 
          
        //  冒泡排序2 
        int []b = {4,9,23,1,45,27,5,2}; 
        int []e = bubbleSort2(b); 
        for(int j=0;j<e.length;j++) 
            System.out.println("冒泡排序2:"+e[j]);    
        } 
    }

  • 相关阅读:
    PHP发送邮件(php100视频后笔记)
    简单页面访问统计
    怎样制作RSS源
    r给自己网站/博客制作安卓应用程序
    反射简单概念
    笔记本
    使用IHttpModule实现简单的页面重映射Url
    工厂方法模式与抽象工厂模式的区别
    安装TFS2010实际体验
    限定登录失败次数,超过指定次数就限制登录一段时间
  • 原文地址:https://www.cnblogs.com/vijozsoft/p/2837969.html
Copyright © 2011-2022 走看看