Java工具包中的Arrays工具类里面有数组的快速排序算法。

源码如下:

 /**
* Sorts the specified range of the array using the given
* workspace array slice if possible for merging
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
* @param work a workspace array (slice)
* @param workBase origin of usable space in work array
* @param workLen usable size of work array
*/
static void sort(int[] a, int left, int right,
int[] work, int workBase, int workLen) {
// Use Quicksort on small arrays
if (right - left < QUICKSORT_THRESHOLD) {
sort(a, left, right, true);
return;
} /*
* Index run[i] is the start of i-th run
* (ascending or descending sequence).
*/
int[] run = new int[MAX_RUN_COUNT + 1];
int count = 0; run[0] = left; // Check if the array is nearly sorted
for (int k = left; k < right; run[count] = k) {
if (a[k] < a[k + 1]) { // ascending
while (++k <= right && a[k - 1] <= a[k]);
} else if (a[k] > a[k + 1]) { // descending
while (++k <= right && a[k - 1] >= a[k]);
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
}
} else { // equal
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
if (--m == 0) {
sort(a, left, right, true);
return;
}
}
} /*
* The array is not highly structured,
* use Quicksort instead of merge sort.
*/
if (++count == MAX_RUN_COUNT) {
sort(a, left, right, true);
return;
}
} // Check special cases
// Implementation note: variable "right" is increased by 1.
if (run[count] == right++) { // The last run contains one element
run[++count] = right;
} else if (count == 1) { // The array is already sorted
return;
} // Determine alternation base for merge
byte odd = 0;
for (int n = 1; (n <<= 1) < count; odd ^= 1); // Use or create temporary array b for merging
int[] b; // temp array; alternates with a
int ao, bo; // array offsets from 'left'
int blen = right - left; // space needed for b
if (work == null || workLen < blen || workBase + blen > work.length) {
work = new int[blen];
workBase = 0;
}
if (odd == 0) {
System.arraycopy(a, left, work, workBase, blen);
b = a;
bo = 0;
a = work;
ao = workBase - left;
} else {
b = work;
ao = 0;
bo = workBase - left;
} // Merging
for (int last; count > 1; count = last) {
for (int k = (last = 0) + 2; k <= count; k += 2) {
int hi = run[k], mi = run[k - 1];
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
b[i + bo] = a[p++ + ao];
} else {
b[i + bo] = a[q++ + ao];
}
}
run[++last] = hi;
}
if ((count & 1) != 0) {
for (int i = right, lo = run[count - 1]; --i >= lo;
b[i + bo] = a[i + ao]
);
run[++last] = right;
}
int[] t = a; a = b; b = t;
int o = ao; ao = bo; bo = o;
}
}

  java.util.Arrays类能方便的操作数组,它所有的方法都是静态的。

  1.filll方法 :给数组中的某段元素附上相同值。

  2.sort方法:对数组中某段元素排序。

  3.equals方法:比较两个数组,判断的是数组中元素值是否相等。

  4.binarySearch方法:对排过序的数组进行二分法查找。

测试用例:

 package recursion;

 import java.util.Arrays;

 /**
* @author zsh
* @company wlgzs
* @create 2019-02-17 9:33
* @Describe Arrays方法测试
*/
public class TestForArrays { public static void main(String[] args) {
//填充数组,将arr[]中所有元素的值初始为0
int[] arr = new int[5];
Arrays.fill(arr,12);
System.out.println(Arrays.toString(arr));
//将arr中的第2个到第三个元素的值赋为8
Arrays.fill(arr,1,3,8);
System.out.println(Arrays.toString(arr));
//对数组进行排序
int[] arr1 = new int[]{7,6,8,5,2,9,8,1,3,5};
//对数组的第二个到第6个元素进行排序
Arrays.sort(arr1,1,6);
System.out.println(Arrays.toString(arr1));
//对整个数组进行排序
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
//比较数组元素是否相等
System.out.println(Arrays.equals(arr,arr1));
//使用二分法在数组中查找指定元素所在的下标
//数组必须是先排好序的
System.out.println(Arrays.binarySearch(arr1,5));
//如果不存在,就返回负数
System.out.println(Arrays.binarySearch(arr1,20));
} }

控制台输出:

最新文章

  1. 数塔问题(DP算法)自底向上计算最大值
  2. 关于JSF中immediate属性的总结(三)
  3. 云端卫士实战录 | Java高级特性之多线程
  4. 使用mysql的长连接
  5. 重构Web Api程序(Api Controller和Entity)
  6. 【leetcode】Combination Sum II
  7. typedef (还需经常看看加深理解)
  8. 设计模式_Iterator_迭代器模式
  9. C# 窗体在线2,8,16进制转换以及,在线更新时间
  10. TCanvas.CopyRect方法中参数CopyMode的意义
  11. 客户端Webview重定向
  12. DOM0 DOM2 DOM3
  13. 一步使你的asp.net网站在手机浏览器上全屏显示
  14. Addrss already in user 解决方案 (linux)
  15. Kali Linux常用服务配置教程DHCP服务原理
  16. Python_字典及其操作
  17. 微软BI 之SSIS 系列 - Lookup 组件的使用与它的几种缓存模式 - Full Cache, Partial Cache, NO Cache
  18. Android.Tools.Ant
  19. OS基础:动态链接库(一)
  20. bzoj千题计划207:bzoj1879: [Sdoi2009]Bill的挑战

热门文章

  1. 软工网络15团队作业4——Alpha阶段敏捷冲刺1.0
  2. sv命令空间 packge
  3. 《大话设计模式》c++实现 之工厂模式
  4. Msfvenom木马使用及TheFatRat工具
  5. html5-表单和input元素用法
  6. jQuery筛选--first()和last()
  7. modelsim仿真正确FPGA运行不正确的可能原因 - cm4写寄存器错
  8. cmd强行终止进程
  9. 20165215 学习基础和c语言基础调查
  10. sublime text3 快捷键和好用的插件