1. package com.tw.ds.sort;  
  2.  
  3.  
  4. /**  
  5.  *<p>java数据结构之:冒泡排序方法  
  6.  *冒泡排序算法的一般性策略:搜索整个值列,比较相邻元素,如果两者的相对次序不对,则交换它们,  
  7.  *   其结果是最大值“想水泡一样”移动到值列的最后一个位置上,  
  8.  *   这也是它在最终完成排序的值列中合适的位置。然后再次搜索值列,  
  9.  *    将第二大的值移动至倒数第二个位置上,重复该过程,直至将所有元素移动到正确的位置上.  
  10.  *    http://lavasoft.blog.51cto.com/  
  11.  *    http://www.blogjava.net/hitlang/archive/2007/09/05/142943.html  
  12.  *    http://tscjsj.blog.51cto.com/412451/84582  
  13.  *    http://liushibin06.blog.163.com/blog/static/170382582009930111519971/  
  14.  *</p>  
  15.  * @author tangw 2010-11-22  
  16.  *  
  17.  */ 
  18. public class BubbleSortMain {  
  19.     //主方法  
  20.     public static void main(String[] args) {  
  21.         //定义数组  
  22.         int[] items = {2,5,1,4,6,100,11,4};  
  23.         //排序  
  24.         sort(items);  
  25.         //循环输出  
  26.         for(int i=0;i<items.length;i++){  
  27.             System.out.println("---i="+i+"   value="+items[i]);  
  28.               
  29.         }  
  30.     }//end method main  
  31.       
  32.     //排序  
  33.     public static void sort(int[] arItems){  
  34.         int temp;  
  35.         for(int i=0;i<arItems.length;++i){  
  36.             for(int j=0;j<arItems.length-i-1;++j){  
  37.                  if(arItems[j] > arItems[j + 1]){  
  38.                      temp = arItems[j];  
  39.                      arItems[j] = arItems[j+1];  
  40.                      arItems[j+1]=temp;  
  41.                  }  
  42.             }  
  43.         }  
  44.     }// end method sort  
  45.  
  46. }