博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java jdk中的归并排序实现
阅读量:4631 次
发布时间:2019-06-09

本文共 2657 字,大约阅读时间需要 8 分钟。

在Arrays.java中的sort中
public static void sort(Object[] a, int fromIndex, int toIndex) {        if (LegacyMergeSort.userRequested)            legacyMergeSort(a, fromIndex, toIndex);        else            ComparableTimSort.sort(a, fromIndex, toIndex);    }    /** To be removed in a future release. */    private static void legacyMergeSort(Object[] a,                                        int fromIndex, int toIndex) {        rangeCheck(a.length, fromIndex, toIndex);        Object[] aux = copyOfRange(a, fromIndex, toIndex);        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);    }    /**     * Tuning parameter: list size at or below which insertion sort will be     * used in preference to mergesort.     * To be removed in a future release.     */    private static final int INSERTIONSORT_THRESHOLD = 7;    /**     * 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     * To be removed in a future release.     */    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
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++]; } }

转载于:https://www.cnblogs.com/zhwj184/archive/2012/11/02/3027455.html

你可能感兴趣的文章
C - 娜娜梦游仙境系列——吃不完的糖果
查看>>
巴黎公社社员造船厂Project1129研制成功
查看>>
poj2007极角排序
查看>>
POJ 1204 Word Puzzles
查看>>
JEESZ分布式框架--单点登录集成方案
查看>>
三元表达式,列表生成式,字典生成式,生成器表达式
查看>>
.net core集成 vue
查看>>
ZOJ3829---模拟,贪心
查看>>
Windows XP系列全下载(均为MSDN原版)
查看>>
如何提高ASP.NET性能
查看>>
vh属性-- 一个永远垂直居中的弹出框
查看>>
LAMP集群项目三 配置业务服务器
查看>>
《Unity_API解析》 第五章 Mathf类
查看>>
计算器
查看>>
批处理备份
查看>>
To the Max(矩阵压缩)
查看>>
数组的学习+冒泡排序
查看>>
C程序设计语言----第1章 导言(一)
查看>>
asp.net文件下载
查看>>
APK IPA --------------- iis7如何添加mime类型支持所有后缀名文件下载的方法(解决特殊后缀文件无法下载的问题)...
查看>>