Java库中的排序方法
来源:互联网 发布:ubuntu 查看文件路径 编辑:程序博客网 时间:2024/06/05 03:27
Java中的java.util.Arrays.sort方法,对于primitive data type采用的是插入排序(元素书目小于7)和快速排序。
对于对象数组的排序采用的是归并排序。
/**
* Sorts the specified sub-array of integers into ascending order.
*/
private static void sort1(int x[], int off, int len) {
// Insertion sort on smallest arrays
if (len < 7) {
for (int i=off; i<len+off; i++)
for (int j=i; j>off && x[j-1]>x[j]; j--)
swap(x, j, j-1);
return;
}
// Choose a partition element, v
int m = off + (len >> 1); // Small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // Big arrays, pseudomedian of 9
int s = len/8;
l = med3(x, l, l+s, l+2*s);
m = med3(x, m-s, m, m+s);
n = med3(x, n-2*s, n-s, n);
}
m = med3(x, l, m, n); // Mid-size, med of 3
}
int v = x[m];
// Establish Invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
while (b <= c && x[b] <= v) {
if (x[b] == v)
swap(x, a++, b);
b++;
}
while (c >= b && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (b > c)
break;
swap(x, b++, c--);
}
// Swap partition elements back to middle
int s, n = off + len;
s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
// Recursively sort non-partition-elements
if ((s = b-a) > 1)
sort1(x, off, s);
if ((s = d-c) > 1)
sort1(x, n-s, s);
}
/**
* Swaps x[a] with x[b].
*/
private static void swap(int x[], int a, int b) {
int t = x[a];
x[a] = x[b];
x[b] = t;
}
/**
* Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
*/
private static void vecswap(int x[], int a, int b, int n) {
for (int i=0; i<n; i++, a++, b++)
swap(x, a, b);
}
/**
* Returns the index of the median of the three indexed integers.
*/
private static int med3(int x[], int a, int b, int c) {
return (x[a] < x[b] ?
(x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
(x[b] > x[c] ? b : x[a] > x[c] ? c : a));
}
* This sort is guaranteed to be <i>stable</i>: equal elements will
* not be reordered as a result of the sort.<p>
*
* The sorting algorithm is a modified mergesort (in which the merge is
* omitted if the highest element in the low sublist is less than the
* lowest element in the high sublist). This algorithm offers guaranteed
* n*log(n) performance.
*
* @param a the array to be sorted
* @throws ClassCastException if the array contains elements that are not
* <i>mutually comparable</i> (for example, strings and integers).
*/
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
- Java库中的排序方法
- java中的排序方法
- java排序方法中的插入排序方法
- java排序方法中的选择排序方法
- Java中的排序比较方法
- Java中的排序方法(冒泡排序 和 选择排序)
- java 数组中的数据排序方法详解
- Java中的几种排序方法
- Java中的几种排序方法
- java中的三种排序方法使用:选择排序,冒泡排序,sort方法排序。
- Java库中的排序函数
- java数组中的三种排序方法中的冒泡排序方法
- OC中的排序方法
- javaScript中的排序方法
- java中的equals和hashCode方法以及集合的排序
- Java中的冒泡排序原理及实现方法
- java 中的 collections.sort() 方法对list的排序
- JAVA 中 Arrays.sort 中的排序的方法
- Image类的扩展,可编辑GIF动画图片,需要Magick支持.
- ibatis的入门例子
- strace命令详解
- Eclipse 3.4 (Ganymede) 开发调试PHP全攻略
- 清除远程桌面信息方法
- Java库中的排序方法
- 应用程序池假死问题解决办法IIS
- 组件对象模型的规则
- Struts核心组件和运行过程
- 正则表达式基础知识
- ASP.NET学习笔记[1] - iis不能使用的问题解决
- ASCII编码表
- MySQL的备份与还原
- 定制Struts控制器组件