`
zhaohaolin
  • 浏览: 984283 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

冒泡排序算法的JAVA实现

阅读更多

package Utils.Sort;  
  
   
  
/** 
 
*@author Linyco 
 
*利用冒泡排序法对数组排序,数组中元素必须实现了Comparable接口。 
 
*/  
public class BubbleSort implements SortStrategy  
  
{  
  
  /** 
 
       *对数组obj中的元素以冒泡排序算法进行排序 
 
       */  
  
       public void sort(Comparable[] obj)  
  
       {     if (obj == null)  
  
              {    throw new NullPointerException("The argument can not be null!");  
  
              }  
              Comparable tmp;  
  
              for (int i = 0 ;i < obj.length ;i++ )  
  
              {    //切记,每次都要从第一个开始比。最后的不用再比。  
  
                     for (int j = 0 ;j < obj.length - i - 1 ;j++ )  
  
                     {   //对邻接的元素进行比较,如果后面的小,就交换  
  
                            if (obj[j].compareTo(obj[j + 1]) > 0)  
  
                            {  tmp = obj[j];  
  
                                   obj[j] = obj[j + 1];  
  
                                   obj[j + 1] = tmp;  
  
                            }   }  
  
   
              }  }  
  
} 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics