implements Comparable接口并重写compareTo方法实现比较与排序
实现Comparable接口并重写compareTo(T o)方法后实现比较排序:
例:
插入排序(它对实现Comparable接口的任何类型的数据进行排序,时间复杂度n^2):
public class Insertion {
@SuppressWarnings({
"rawtypes", "unchecked"})
public static void sort(Comparable[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
for (int j = i; j > 0; j--) {
if (a[j-1].compareTo(a[j]) > 0) {
exch(a, j-1, j);
}
else break;
}
}
}
// exchange a[i] and a[j]
@SuppressWarnings("rawtypes")
private static void exch(Comparable[] a, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
// read in a sequence of words from standard input and print
// them out in sorted order
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
sort(a);
for (int i = 0; i < a.length; i++) {
StdOut.print(a[i] + " ");
}
StdOut.println();
}
}
例如我们在counter类中实现comparable接口并实现了CompareTo()方法以期进行比较排序:
public class Counter implements Comparable<Counter> {
private final String name; // counter name
private final int maxCount; // maximum value
private int count; // current value
// create a new counter with the given parameters
public Counter(String id, int max) {
name = id;
maxCount = max;
count = 0;
}
// compare two Counter objects based on their count
public int compareTo(Counter that) {
if (this.count < that.count) return -1;
else if (this.count > that.count) return +1;
else return 0;
}
}
归并排序(它对实现Comparable接口的任何类型的数据进行排序,时间复杂度nlgn,但空间上需要Comparable[] aux这样一个与原数组等大小的数组空间作为开销)(jdk排序中采用了归并排序方法进行排序实现):
public class Merge {
/*************************************************************************** * Merge the subarrays a[lo] .. a[mid-1] and a[mid] .. a[hi-1] into * a[lo] .. a[hi-1] using the auxilliary array aux[] as scratch space. * * Precondition: the two subarrays are in ascending order * Postcondition: a[lo] .. a[hi-1] is in ascending order * ***************************************************************************/
@SuppressWarnings({
"rawtypes", "unchecked"})
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
int i = lo, j = mid;
for (int k = lo; k < hi; k++) {
if (i == mid) aux[k] = a[j++];
else if (j == hi) aux[k] = a[i++];
else if (a[j].compareTo(a[i]) < 0) aux[k] = a[j++];
else aux[k] = a[i++];
}
// copy back
for (int k = lo; k < hi; k++)
a[k] = aux[k];
}
/*************************************************************************** * Mergesort the subarray a[lo] .. a[hi-1], using the * auxilliary array aux[] as scratch space. ***************************************************************************/
@SuppressWarnings("rawtypes")
public static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) {
// base case
if (hi - lo <= 1) return;
// sort each half, recursively
int mid = lo + (hi - lo) / 2;
sort(a, aux, lo, mid);
sort(a, aux, mid, hi);
// merge back together
merge(a, aux, lo, mid, hi);
}
/*************************************************************************** * Sort the array a using mergesort. ***************************************************************************/
@SuppressWarnings("rawtypes")
public static void sort(Comparable[] a) {
int n = a.length;
Comparable[] aux = new Comparable[n];
sort(a, aux, 0, n);
}
/*************************************************************************** * Sort the subarray a[lo..hi] using mergesort. ***************************************************************************/
@SuppressWarnings("rawtypes")
public static void sort(Comparable[] a, int lo, int hi) {
int n = hi - lo + 1;
Comparable[] aux = new Comparable[n];
sort(a, aux, lo, hi);
}
/*************************************************************************** * Check if array is sorted - useful for debugging. ***************************************************************************/
@SuppressWarnings({
"rawtypes", "unchecked"})
private static boolean isSorted(Comparable[] a) {
for (int i = 1; i < a.length; i++)
if (a[i].compareTo(a[i-1]) < 0) return false;
return true;
}
/*************************************************************************** * Show results. ***************************************************************************/
@SuppressWarnings("rawtypes")
public static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
}
/*************************************************************************** * Test client. ***************************************************************************/
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Merge.sort(a);
assert isSorted(a);
for (int i = 0; i < a.length; i++) {
StdOut.print(a[i] + " ");
}
StdOut.println();
}
}