內置類比較
Comparable 接口
Comparable 接口的實現
- 通過實現 java.lang.Comparable 接口確定該類對象的排序方式
Comparable 接口中只有一個方法:
該方法的返回值定義如下:public interface Comparable<T> { public int compareTo(T o); }
舉例:在Integer包裝類中返回值 = 0 表示 this == o 返回值 > 0 表示 this > o 返回值 < 0 表示 this < o
public final class Integer extends Number implements Comparable<Integer> { ··· public int compareTo(Integer anotherInteger) { return compare(this.value, anotherInteger.value); } public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); } ··· }
常見引用類型的 Comparable 接口實現原理
- 基本數據類型的包裝類:直接比較基本數據類型的大小
- 字符:比較Unicode碼之差
- 字符串:
- 如果一個字符串以另一個字符串為開始,則返回兩個字符串的長度差
- 否則返回第一個不相等的字符的Unicode碼之差
- Date類型:根據日期的長整型數比較
compareTo () 方法的使用
- 在排序或比較時通過compareTo函數的返回值決定順序
舉例:冒泡排序中compareTo 方法的使用
舉例:調用Collections工具類中提供的sort()方法/** * 需要注意的是這里使用的泛型 T 必須實現接口 Comparable * T extends Comparable<T> 等同于 T extends Comparable<? super T> * 其含義為 T 是實現了Comparable的一個類的子類 */ public static <T extends Comparable<T>> void sort(T[] array) { for (int i = 0; i < array.length; i++) { for (int j = array.length - 2; j >= i; j--) { if (array[j].compareTo(array[j + 1]) > 0) { swap(array, j, j + 1); } } } } public static <T> void swap(T[] array, int a, int b){ T tmp = array[b]; array[b] = array[a]; array[a] = tmp; }
public class Collections { ··· public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(c); } ··· } public static void main(String[] args) { List<Student> stu = new ArrayList(); ··· Collections.sort(stu); }
Comparator 接口
Comparator接口的實現
- 通過實現 java.util.Comparator 接口確中的 compare(T o1, T o2)方法實現對象的比較
舉例:在學生的個人信息中按照分數對學生類型進行比較public interface Comparator<T> { public int compare(T o1, T o2); }
class Student { String name; int classNo; public int score; ··· public int getScore() { return score; } ··· } class StudentSort implements java.util.Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getScore() - o2.getScore(); } }
compare() 方法的使用
- 在排序或比較時新建一個比較器,通過比較器調用compare()方法
舉例:通過成績對學生進行排序
舉例:調用Collections工具類中提供的sort()方法并指定比較器public static void SortByScore(Student[] stu){ StudentSort ss = new StudentSort(); ··· if(ss.compare(stu[i], stu[i + 1]) > 0){ ··· } ··· }
public class Collections { ··· public static <T> void sort(List<T> list, Comparator<? super T> c) { list.sort(c); } ··· } public static void main(String[] args) { List<Student> stu = new ArrayList(); ··· Collections.sort(stu, new StudentSort()); }
Comparable 和 Comparator 的區別
位置 | 特點 | |
---|---|---|
Comparable | java.lang | 屬于對象本身的方法,允許對象與自己比較,與類高耦合 |
Comparator | java.util | 獨立于對象之外的方法,不允許對象與自己比較,與類低耦合 |
適用情況 | |
---|---|
Comparable | 對象屬性簡單,對象適合與自身比較且邏輯簡單 |
Comparator | 對象屬性復雜,對象無法和自身直接比較,或需要提供若干種比較方法 |
排序算法
這里關注的僅僅是如何進行排序而非比較,所以示例程序中的對象只選用整數
算法中經常會用到 swap() 方法交換兩個元素的位置
public static <T> void swap(T[] array, int a, int b){
T tmp = array[b];
array[b] = array[a];
array[a] = tmp;
}
冒泡排序
冒泡排序(Bubble Sort)是一種交換排序,它的基本思想是:兩兩比較相鄰記錄的關鍵字,如果反序則交換,直到沒有反序的記錄為止。
標準冒泡排序
算法原理
-
對數組進行兩層循環遍歷。讓數組中較小的關鍵字能夠較快地移動到數組的頂部,從而當兩層循環結束,排序即可完成。
算法實現
void bubbleCore(int[] array) {
int arrayLength = array.length;
for (int i = 0; i < arrayLength; i++) {
for (int j = arrayLength - 2; j >= i; j--) {
if (array[j] > array[j + 1]) {
swap(array, j, j + 1);
}
}
}
}
改進冒泡排序
算法原理
- 標準冒泡排序在數組已經有序后依舊進行比較操作(不進行交換操作),直至循環結束
-
這里使用一個標志位,來標識當前數組是否已經有序。如果無序,則繼續冒泡排序;如果已經有序,則退出排序算法。這樣就可以很好地規避掉一些不必要的比較操作
算法實現
void bubbleCoreAdvanced(int[] array) {
boolean status = true; // 記錄是否發生交換(發生為ture,未發生為false)
int arrayLength = array.length;
for (int i = 0; i < arrayLength && status; i++) {
status = false;
for (int j = arrayLength - 2; j >= i; j--) {
if (array[j] > array[j + 1]) {
swap(array, j, j + 1);
status = true;
}
}
}
}
雙向冒泡排序
算法原理
- 在單向冒泡排序算法中,存在著一個著名的“烏龜問題”,即假設我們需要將序列A按照升序序列排序。序列中的較小的數字又大量存在于序列的尾部,這樣會讓小數字在向前移動得很緩慢。
- 基于冒泡排序的基礎上,無論是從前向后遍歷交換,還是從后向前遍歷交換,對程序的邏輯和性能的代價都是不影響的,故可以讓一部分情況下從前向后遍歷交換,另一部分情況從后向前遍歷交換。
- 比較相鄰兩個元素的大小。如果前一個元素比后一個元素大,則兩元素位置交換
- 對數組中所有元素的組合進行第1步的比較
- 奇數趟時從左向右進行比較和交換
- 偶數趟時從右向左進行比較和交換
- 當從左端開始遍歷的指針與從右端開始遍歷的指針相遇時,排序結束
算法實現
static void doubleBubbleCore(int[] array){
int left = 0;
int right = array.length - 1;
boolean status = false;// 記錄是否發生交換(發生為ture,未發生為false)
while (left < right){
for (int i = left; i < right; i++){
if (array[i] > array[i+1]){
swap(array, i, i + 1);
status = true;
}
}
right --;
for (int i = right-1; i >= left; i--){
if (array[i] > array[i+1]){
swap(array, i, i + 1);
status = true;
}
}
left ++;
if(!status){
break; //未發生交換表示排序結束,退出循環
}
}
}