Java Arrays工具类源码算法解析
Java SE中有自带的两个常用的工具类Arrays类(针对数据相关的处理)、Math类(针对数字计算相关的处理),由于Math类的函数大致与C++等编程语言相同,底层的介绍显得意义并不大,只要是学习过编程的程序猿想必对Maths中的函数并不陌生,故本篇博文将以Arrays类为重点,并着重介绍Arrays类中的sort方法的底层算法实现,而Math类的介绍将重点从使用的角度介绍以下常用的API即可。下面,嘿嘿!造作吧~~~~
(温馨提示:源码的介绍中将会涉及到多种排序查找等算法,需具备一定的数据结构与算法基本功。如有不适请点击~数据结构与算法—数组~)
Arrays篇
Arrays类结构
Arrays工具类的设计就是为了帮助开发者非常方便的使用Arrays类中的方法来处理数组对象。下面我将从Arrays类中最值得分析的binarySearch()方法与sort()方法入手展开。
binarySearch()方法分析
binarySearch()方法:从binary search不难看出这是一个用二分查找算法实现的方法,能快速定位出要查找数组中指定数值的具体位置,若不存在指定数值返回负值。源码如下图:
从以上两个图中可以看出,binarySearch方法中调用了binarySearch0()方法,我们阅读binarySearch0()方法的源码可以看出其使用的正是二分查找算法;由此可以看出binarySearch方法的执行效率还是可以的,但是不足之处正是二分查找的缺点:待查找的数组必须为有序数组。
sort()方法分析
sort()方法:Arrays类中的sort方法用来进行数组的排序,这是一个综合性的算法实现方法,在sort()方法中融合了快排、选择排序等。sort方法对于基本数据类型(7种)采用归并排序、插入排序、快排算法、计数排序,而对于自定义类型则采用选择排序。进行这样区分的原因主要是不同类型的数据对稳定性的要求不同,基本数据类型只涉及数、字符的排序,不需要满足稳定性,于是选择更加快速的不稳定算法快排;而自定义类型数据需要保证稳定性,故不采用快排,而采用稳定性算法插入排序、归并排序。
基本数据类型参数&源码清单:
/*
* Tuning parameters.
*/
/**
* The maximum number of runs in merge sort.归并排序
*/
private static final int MAX_RUN_COUNT = 67;
/**
* The maximum length of run in merge sort.归并排序
*/
private static final int MAX_RUN_LENGTH = 33;
/**
* If the length of an array to be sorted is less than this
* constant, Quicksort is used in preference to merge sort.快排
*/
private static final int QUICKSORT_THRESHOLD = 286;
/**
* If the length of an array to be sorted is less than this
* constant, insertion sort is used in preference to Quicksort.插入排序
*/
private static final int INSERTION_SORT_THRESHOLD = 47;
/**
* If the length of a byte array to be sorted is greater than this
* constant, counting sort is used in preference to insertion sort.计数排序
*/
private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;
/**
* If the length of a short or char array to be sorted is greater
* than this constant, counting sort is used in preference to Quicksort.计数排序
*/
private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
从以上参数清单中不难看出,sort方法对于基本数据类型共采用了4种排序算法,这4种排序算法的选取是按照传入数组的取值范围和数组的长度来进行最优选取的;在从源码清单中可以看出在进行确切的排序算法之前都是先进行if条件判断数组取值或长度来选取特定的算法的。
自定义数据类型参数&源码清单:
对于自定义数据类型的sort排序方法,有两种选择①归并排序夹杂插入排序;②TimSort排序,这是归并排序与插入排序的结合体(目前还没弄懂,弄懂后将更新此段内容)
以下内容是方法①的源代码:其中INSERTIONSORT_THRESHOLD=7参数作为进行选择插入排序和归并排序的依据
@SuppressWarnings({"unchecked", "rawtypes"})
private static void 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;
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++];
}
}
以下内容是方法②的源代码:Timsort.sort()源码
/**
* Sorts the given range, using the given workspace array slice
* for temp storage when possible. This method is designed to be
* invoked from public methods (in class Arrays) after performing
* any necessary array bounds checks and expanding parameters into
* the required forms.
*
* @param a the array to be sorted
* @param lo the index of the first element, inclusive, to be sorted
* @param hi the index of the last element, exclusive, to be sorted
* @param c the comparator to use
* @param work a workspace array (slice)
* @param workBase origin of usable space in work array
* @param workLen usable size of work array
* @since 1.8
*/
static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length;
int nRemaining = hi - lo;
if (nRemaining < 2)
return; // Arrays of size 0 and 1 are always sorted
// If array is small, do a "mini-TimSort" with no merges
if (nRemaining < MIN_MERGE) {
int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
binarySort(a, lo, hi, lo + initRunLen, c);
return;
}
/**
* March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant.
*/
TimSort<T> ts = new TimSort<>(a, c, work, workBase, workLen);
int minRun = minRunLength(nRemaining);
do {
// Identify next run
int runLen = countRunAndMakeAscending(a, lo, hi, c);
// If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun) {
int force = nRemaining <= minRun ? nRemaining : minRun;
binarySort(a, lo, lo + force, lo + runLen, c);
runLen = force;
}
// Push run onto pending-run stack, and maybe merge
ts.pushRun(lo, runLen);
ts.mergeCollapse();
// Advance to find next run
lo += runLen;
nRemaining -= runLen;
} while (nRemaining != 0);
// Merge all remaining runs to complete sort
assert lo == hi;
ts.mergeForceCollapse();
assert ts.stackSize == 1;
}
待续。。。
友情链接1:https://www.jianshu.com/p/10aa41b780f2
友情链接2:https://blog.csdn.net/yangzhongblog/article/details/8184707
Math篇
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。下面列出常用API:
成员方法
* public static int abs(int a)——取绝对值
* public static double ceil(double a)——向上取整
* public static double floor(double a)——向下取整
* public static int max(int a,int b)——取最大值
* public static double pow(double a,double b)——a的b次方
* public static double random()——取随机值
* public static int round(float a) ——四舍五入取整
* public static double sqrt(double a)——开平方 * public static double sin(double a)——三角函数
这里值得注意的是用Math类中的方法操作的数值的范围,最特殊的点是最值(最大值、最小值);这也是本人在做算法题的时候发现的问题,先分享出来:(以int类型求abs为例)
待定。。。从上图我们发现在abs下int的最小值min_value无法变成+2147483648,了解计算机原理的想必已近知道了其中的原因。因为int类型的值的范围在-2147483648~2147483647之间,也就无法取的范围以外的值了。
谢谢阅读 ----知飞翀