首页 > 编程 > Java > 正文

冒泡排序算法的JAVA实现

2019-11-17 06:31:39
字体:
来源:转载
供稿:网友

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



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表