public static void sort(float[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); sort2(a, fromIndex, toIndex); } private static void sort2(double a[], int fromIndex, int toIndex) { final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d); /* * The sort is done in three phases to avoid the expense of using * NaN and -0.0 aware comparisons during the main sort. */ /* * Preprocessing phase: Move any NaN's to end of array, count the * number of -0.0's, and turn them into 0.0's. */ int numNegZeros = 0; int i = fromIndex, n = toIndex; while(i < n) { if (a[i] != a[i]) { double swap = a[i]; a[i] = a[--n]; a[n] = swap; } else { if (a[i]==0 && Double.doubleToLongBits(a[i])==NEG_ZERO_BITS) { a[i] = 0.0d; numNegZeros++; } i++; } } 快速排序吧!
即基本类型数据(如int,short,long等)排序,合并排序用于对对象类型进行排序。
对对象数组排序方法的名字是mergeSort
rangeCheck(a.length, fromIndex, toIndex);
sort2(a, fromIndex, toIndex);
} private static void sort2(double a[], int fromIndex, int toIndex) {
final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d);
/*
* The sort is done in three phases to avoid the expense of using
* NaN and -0.0 aware comparisons during the main sort.
*/ /*
* Preprocessing phase: Move any NaN's to end of array, count the
* number of -0.0's, and turn them into 0.0's.
*/
int numNegZeros = 0;
int i = fromIndex, n = toIndex;
while(i < n) {
if (a[i] != a[i]) {
double swap = a[i];
a[i] = a[--n];
a[n] = swap;
} else {
if (a[i]==0 && Double.doubleToLongBits(a[i])==NEG_ZERO_BITS) {
a[i] = 0.0d;
numNegZeros++;
}
i++;
}
}
快速排序吧!