# 结合Comparable接口优化排序，给新员工

代码非常简单，主要是提醒大家能够在写代码时候，时时刻刻谨记。

```package org.dxy.util;

import java.lang.reflect.Array;

public class MergeSort {

public static void main(String[] args) {
Integer[] array = new Integer[] { 11111, 24, 90, 234, 15, 478, 12, 55,
901, 213, 56, 11, 23, 4, 5, 67, 113, 34 };
sort(array);
for (Integer val : array) {
System.out.println(val);
}
}

@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(T[] a) {
T[] helper = (T[]) Array.newInstance(a[0].getClass(), a.length);
mergesort(a, helper, 0, a.length - 1);
}

private static <T extends Comparable<? super T>> void mergesort(T[] a,
T[] helper, int lo, int hi) {
if (lo >= hi)
return;
int mid = lo + (hi - lo) / 2;
mergesort(a, helper, lo, mid);
mergesort(a, helper, mid + 1, hi);
merge(a, helper, lo, mid, hi);
}

private static <T extends Comparable<? super T>> void merge(T[] a,
T[] helper, int lo, int mid, int hi) {
for (int i = lo; i <= hi; i++) {
helper[i] = a[i];
}
int i = lo, j = mid + 1;
for (int k = lo; k <= hi; k++) {
// 左边的超出范围，就选择右边
if (i > mid) {
System.out.println("i :" + i + " mid :" + mid);
a[k] = helper[j++];
}
// 右边超出范围，选择左边
else if (j > hi) {
System.out.println("j :" + j + " hi :" + hi);
a[k] = helper[i++];
} else if (isLess(helper[i], helper[j])) {
a[k] = helper[i++];
} else {
a[k] = helper[j++];
}
}
}

private static <T extends Comparable<? super T>> boolean isLess(T a, T b) {
//可以采用模板的方式，子类重写改方法，进行比较
return a.compareTo(b) < 0;
}
}
```

```package org.dxy.util;

import java.lang.reflect.Array;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

class MergeSortTask<T extends Comparable<? super T>> extends RecursiveAction {
private static final long serialVersionUID = -749935388568367268L;
private final T[] a;
private final T[] helper;
private final int lo;
private final int hi;

public MergeSortTask(T[] a, T[] helper, int lo, int hi) {
this.a = a;
this.helper = helper;
this.lo = lo;
this.hi = hi;
}

@Override
protected void compute() {
if (lo >= hi)
return;
int mid = lo + (hi - lo) / 2;
invokeAll(left, right);
merge(this.a, this.helper, this.lo, mid, this.hi);

}

private void merge(T[] a, T[] helper, int lo, int mid, int hi) {
for (int i = lo; i <= hi; i++) {
helper[i] = a[i];
}
int i = lo, j = mid + 1;
for (int k = lo; k <= hi; k++) {
if (i > mid) {
a[k] = helper[j++];
} else if (j > hi) {
a[k] = helper[i++];
} else if (isLess(helper[i], helper[j])) {
a[k] = helper[i++];
} else {
a[k] = helper[j++];
}
}
}

private boolean isLess(T a, T b) {
return a.compareTo(b) < 0;
}

@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(T[] a) {
T[] helper = (T[]) Array.newInstance(a[0].getClass(), a.length);
ForkJoinPool forkJoinPool = new ForkJoinPool(10);
forkJoinPool.invoke(new MergeSortTask<T>(a, helper, 0, a.length - 1));
}

public static void main(String[] args) {
Integer[] array = new Integer[] { 1, 24, 90, 234, 15, 478, 12, 55,
901, 213, 56, 11, 23, 4, 5, 67, 113, 34 };
sort(array);
for (Integer val : array) {
System.out.println(val);
}
}

}
```

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊

/* 需求：冒泡排序的实现 思路： 1，冒泡排序的基本思想是：两两比较相邻记录的关键字，如果反序则