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]);    
        } 
    }

  • 相关阅读:
    CFree 提示main must return int
    策略模式
    CFree 提示no newline at the end of file
    EEPROM的写入操作解析
    一些关于mic的知识
    一些关于电池的资料
    太阳能电池板日发电量简易计算方法
    ubuntu 下载编译android源代码
    SC44B0的内存地址解析
    RequireJS 2.0 学习笔记一
  • 原文地址:https://www.cnblogs.com/vijozsoft/p/2837969.html
Copyright © 2011-2022 走看看