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

posted @ 2012-12-28 22:31  栗子·无限意志  阅读(166)  评论(0编辑  收藏  举报