数组中排序
public static void main(String[] args) {
int[] nums = {1,3,5,2,7,6,9};
Arrays.sort(nums);
System.out.println("增序排列");
for (int i = 0; i < nums.length; i++) {
System.out.println("nums:" + i + " :" + nums[i]);
}
//要实现减序排序,得通过包装类型数组,基本类型数组是不行的
Integer[] integers = new Integer[]{2,555,3,789,4,1,543};
System.out.println("减序排列");
//通过一个比较器,比较函数可以通过比较进而
Arrays.sort(integers, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
System.out.println("部分增序排列");
for (int i = 0; i < nums.length; i++) {
System.out.println("integers:" + i + " :" + integers[i]);
}
int[] ints = {1222,77,5,3,444,76,7};
// 对数组的[2,5)位进行排序
Arrays.sort(ints,2,5);
for (int i = 0; i < ints.length; i++) {
System.out.println("ints:" + i + " :" + ints[i]);
}
}
比较函数
一个比较函数,它对某些对象集合进行总排序。比较器可以传递给排序方法(例如Collections.sort或Arrays.sort )以允许精确控制排序顺序。
package java.util;
public class Arrays {
// 排序
public static void sort(int[] a) {
DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}
// 部分排序
public static void sort(int[] a, int fromIndex, int toIndex) {
rangeCheck(a.length, fromIndex, toIndex);
DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
}
}
DualPivotQuicksort 双轴快速排序
/**
* 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
*/
/**
*使用给定的
*工作区数组切片(如果可能)用于合并
*@param a要排序的数组
*@param将第一个元素的索引(包括)留作排序
*@param right要排序的最后一个元素(包括)的索引
*@param工作空间数组(切片)
*@param workBase工作阵列中可用空间的来源
*@param worken工作数组的可用大小
*/
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;
}
}
https://blog.csdn.net/github_38838414/article/details/80642329
注意:博客中的非27,根据源码为47