当前位置: 编程技术>java/j2ee
Java 位图法排序的使用方法
来源: 互联网 发布时间:2014-10-23
本文导语: java JDK里面容器类的排序算法使用的主要是插入排序和归并排序,可能不同版本的实现有所不同,关键代码如下: 代码如下:/** * Performs a sort on the section of the array between the given indices * using a mergesort with exponential search...
java JDK里面容器类的排序算法使用的主要是插入排序和归并排序,可能不同版本的实现有所不同,关键代码如下:
代码如下:
/**
* Performs a sort on the section of the array between the given indices
* using a mergesort with exponential search algorithm (in which the merge
* is performed by exponential search). n*log(n) performance is guaranteed
* and in the average case it will be faster then any mergesort in which the
* merge is performed by linear search.
*
* @param in -
* the array for sorting.
* @param out -
* the result, sorted array.
* @param start
* the start index
* @param end
* the end index + 1
*/
@SuppressWarnings("unchecked")
private static void mergeSort(Object[] in, Object[] out, int start,
int end) {
int len = end - start;
// use insertion sort for small arrays
if (len start
&& current.compareTo(prev = out[j - 1]) < 0);
out[j] = current;
}
}
return;
}
int med = (end + start) >>> 1;
mergeSort(out, in, start, med);
mergeSort(out, in, med, end);
// merging
// if arrays are already sorted - no merge
if (((Comparable) in[med - 1]).compareTo(in[med]) 0);
// copy rest of array
if ((end - r)