冒泡排序算法的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; } } } } }?