Java Arrays.sort源代码解析
来源:互联网 发布:中经商品交易中心软件 编辑:程序博客网 时间:2024/05/16 11:00
基本类型:采用调优的快速排序(Primitive(int,float等原型数据));
对象类型:采用改进的归并排序(Object对象数组)。
源码中选择划分元的方法:
当数组大小为 size=7 时 ,取数组中间元素作为划分元。int n=m>>1;(此方法值得借鉴)
当数组大小7< size<=40时,取首、中、末三个元素中间大小的元素作为划分元。
当数组大小 size>40 时 ,从待排数组中较均匀的选择9个元素,选出一个伪中数做为划分元。
根据划分元 v ,形成不变式 v* (v)* v*
普通的快速排序算法,经过一次划分后,将划分元排到素组较中间的位置,左边的元素小于划分元,右边的元素大于划分元,而没有将与划分元相等的元素放在其附近,这一点,在Arrays.sort()中得到了较大的优化。
举例:15、93、15、41、6、15、22、7、15、20
因 7
经过一次换分后: 15、15、7、6、41、20、22、93、15、15. 与划分元相等的元素都移到了素组的两边。
接下来将与划分元相等的元素移到数组中间来,形成:7、6、15、15、15、15、41、20、22、93.
最后递归对两个区间进行排序[7、6]和[41、20、22、93].
package com.util;
import java.lang.reflect.Array;
publicclassArraysObject{
privatestaticfinalint INSERTIONSORT_THRESHOLD=7;
privateArraysObject(){}
publicstaticvoid sort(Object[] a){
//java.lang.Object.clone(),理解深表复制和浅表复制
Object[] aux =(Object[]) a.clone();
mergeSort(aux, a,0, a.length,0);
}
publicstaticvoid sort(Object[] a,int fromIndex,int toIndex){
rangeCheck(a.length, fromIndex, toIndex);
Object[] aux = copyOfRange(a, fromIndex, toIndex);
mergeSort(aux, a, fromIndex, toIndex,-fromIndex);
}
/**
* Src is the source array that starts at index 0
* Dest is the (possibly larger) array destination with a possible offset
* low is the index in dest to start sorting
* high is the end index in dest to end sorting
* off is the offset to generate corresponding low, high in src
*/
privatestaticvoid mergeSort(Object[] src,Object[] dest,int low,
int high,int off){
int length = high- low;
// Insertion sort on smallest arrays
if(length< INSERTIONSORT_THRESHOLD){
for(int i= low; i< high; i++)
for(int j= i; j> low&&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low+= off;
high+= off;
/*
* >>>:无符号右移运算符
* expression1 >>> expresion2:expression1的各个位向右移expression2
* 指定的位数。右移后左边空出的位数用0来填充。移出右边的位被丢弃。
* 例如:-14>>>2; 结果为:1073741820
*/
int mid =(low+ high)>>>1;
mergeSort(dest, src, low, mid,-off);
mergeSort(dest, src, mid, high,-off);
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if(((Comparable) src[mid-1]).compareTo(src[mid])<=0){
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i= destLow, p= low, q= mid; i< destHigh; i++){
if(q>= high|| p< mid
&&((Comparable) src[p]).compareTo(src[q])<=0)
dest[i]= src[p++];
else
dest[i]= src[q++];
}
}
/**
* Check that fromIndex and toIndex are in range, and throw an appropriate
* exception if they aren't.
*/
privatestaticvoid rangeCheck(int arrayLen,int fromIndex,int toIndex){
if(fromIndex> toIndex)
thrownewIllegalArgumentException("fromIndex("+ fromIndex
+") > toIndex("+ toIndex+")");
if(fromIndex<0)
thrownewArrayIndexOutOfBoundsException(fromIndex);
if(toIndex> arrayLen)
thrownewArrayIndexOutOfBoundsException(toIndex);
}
publicstatic<T> T[] copyOfRange(T[] original,int from,int to){
return copyOfRange(original, from, to,(Class<T[]>) original.getClass());
}
publicstatic<T, U> T[] copyOfRange(U[] original,int from,int to,
Class<?extends T[]> newType){
int newLength = to- from;
if(newLength<0)
thrownewIllegalArgumentException(from+" > "+ to);
T[] copy =((Object) newType==(Object)Object[].class)
?(T[])newObject[newLength]
:(T[])Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy,0,
Math.min(original.length- from, newLength));
return copy;
}
/**
* Swaps x[a] with x[b].
*/
privatestaticvoid swap(Object[] x,int a,int b){
Object t = x[a];
x[a]= x[b];
x[b]= t;
}
}
对于Object类型源码分析如下
package com.util;
import java.lang.reflect.Array;
publicclassArraysObject{
privatestaticfinalint INSERTIONSORT_THRESHOLD=7;
privateArraysObject(){}
publicstaticvoid sort(Object[] a){
//java.lang.Object.clone(),理解深表复制和浅表复制
Object[] aux =(Object[]) a.clone();
mergeSort(aux, a,0, a.length,0);
}
publicstaticvoid sort(Object[] a,int fromIndex,int toIndex){
rangeCheck(a.length, fromIndex, toIndex);
Object[] aux = copyOfRange(a, fromIndex, toIndex);
mergeSort(aux, a, fromIndex, toIndex,-fromIndex);
}
/**
* Src is the source array that starts at index 0
* Dest is the (possibly larger) array destination with a possible offset
* low is the index in dest to start sorting
* high is the end index in dest to end sorting
* off is the offset to generate corresponding low, high in src
*/
privatestaticvoid mergeSort(Object[] src,Object[] dest,int low,
int high,int off){
int length = high- low;
// Insertion sort on smallest arrays
if(length< INSERTIONSORT_THRESHOLD){
for(int i= low; i< high; i++)
for(int j= i; j> low&&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low+= off;
high+= off;
/*
* >>>:无符号右移运算符
* expression1 >>> expresion2:expression1的各个位向右移expression2
* 指定的位数。右移后左边空出的位数用0来填充。移出右边的位被丢弃。
* 例如:-14>>>2; 结果为:1073741820
*/
int mid =(low+ high)>>>1;
mergeSort(dest, src, low, mid,-off);
mergeSort(dest, src, mid, high,-off);
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if(((Comparable) src[mid-1]).compareTo(src[mid])<=0){
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i= destLow, p= low, q= mid; i< destHigh; i++){
if(q>= high|| p< mid
&&((Comparable) src[p]).compareTo(src[q])<=0)
dest[i]= src[p++];
else
dest[i]= src[q++];
}
}
/**
* Check that fromIndex and toIndex are in range, and throw an appropriate
* exception if they aren't.
*/
privatestaticvoid rangeCheck(int arrayLen,int fromIndex,int toIndex){
if(fromIndex> toIndex)
thrownewIllegalArgumentException("fromIndex("+ fromIndex
+") > toIndex("+ toIndex+")");
if(fromIndex<0)
thrownewArrayIndexOutOfBoundsException(fromIndex);
if(toIndex> arrayLen)
thrownewArrayIndexOutOfBoundsException(toIndex);
}
publicstatic<T> T[] copyOfRange(T[] original,int from,int to){
return copyOfRange(original, from, to,(Class<T[]>) original.getClass());
}
publicstatic<T, U> T[] copyOfRange(U[] original,int from,int to,
Class<?extends T[]> newType){
int newLength = to- from;
if(newLength<0)
thrownewIllegalArgumentException(from+" > "+ to);
T[] copy =((Object) newType==(Object)Object[].class)
?(T[])newObject[newLength]
:(T[])Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy,0,
Math.min(original.length- from, newLength));
return copy;
}
/**
* Swaps x[a] with x[b].
*/
privatestaticvoid swap(Object[] x,int a,int b){
Object t = x[a];
x[a]= x[b];
x[b]= t;
}
}
阅读全文
0 0
- Java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- Java Arrays.sort源代码解析
- java 解析java.util.Arrays.sort
- java Arrays.sort()排序
- java arrays sort
- java源码分析:Arrays.sort
- [Java]Arrays.sort() 数组排序
- Java数组排序Arrays.sort
- java Arrays.sort 源码分析
- Java Arrays.sort() on objects
- JAVA排序算法 -- Arrays.sort()
- Centos6下SSH免密码连接配置
- Socket
- 20170812
- HDU6108 小C的倍数问题 【脑洞】
- SPOJ
- Java Arrays.sort源代码解析
- GetMemory 改错
- 30分钟全面解析-SQL事务+隔离级别+阻塞+死锁
- Java基础002
- 安卓——问题:控件(view)的OnTouch事件只能获取到ACTION_DOWN事件,UP、MOVE等事件都无法获取到
- 2018网易秋招内推笔试题——独立的小易
- 八大排序算法总结
- Big-man进军Linux系统(一)
- 索引失效的几种例子