• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java 位图法排序的使用方法

java 搞代码 4年前 (2022-01-05) 15次浏览 已收录 0个评论

本篇文章,小编将为大家介绍关于Java 位图法排序的使用方法,有需要的朋友可以参考一下

java JDK里面容器类的排序算法使用的主要是插入排序和归并排序,可能不同版本的实现有所不同,关键代码如下:

代码如下:
/**
     * Performs a sort on the section of the array between the given indices
     * using a mergesort with exponential search algorithm (in which the merge
     * is performed by exponential search). n*log(n) performance is guaranteed
     * and in the average case it will be faster then any mergesort in which the
     * merge is performed by linear search.
     *
     * @param in –
     *            the array for sorting.
     * @param out –
     *            the result, sorted array.
     * @param start
     *            the start index
     * @param end
     *            the end index + 1
     */
    @SuppressWarnings(“unchecked”)
    private static void mergeSort(Object[] in, Object[] out, int start,
            int end) {
        int len = end – start;
        // use insertion sort for small arrays
        if (len <= SIMPLE_LENGTH) {
            for (int i = start + 1; i <end; i++) {
                Comparable current = (Comparable) out[i];
                Object prev = out[i – 1];
                if (current.compareTo(prev) <0) {
                    int j = i;
                    do {
                        out[j–] = prev;
                    } while (j > start
                            && current.compareTo(prev = out[j – 1]) <0);
                    out[j] = current;
                }
            }
            return;
        }
        int med = (end + start) >>> 1;
        mergeSort(out, in, start, med);
        mergeSort(out, in, med, end);

        // merging

        // if arrays are already sorted – no merge
        if (((Comparable) in[med – 1]).compareTo(in[med]) <= 0) {
            System.arraycopy(in, start, out, start, len);
            return;
        }
        int r = med, i = start;

        // use merging with exponential search
        do {
            Comparable fromVal = (Comparable) in[start];
            Comparable rVal = (Comparable) in[r];
            if (fromVal.compareTo(rVal) <= 0) {
                int l_1 = find(in, rVal, -1, start + 1, med – 1);
                int toCopy = l_1 – start + 1;
                System.arraycopy(in, start, out, i, toCopy);
                i += toCopy;
                out[i++] = rVal;
                r++;
                start = l_1 + 1;
            } else {
                int r_1 来源gao@!dai!ma.com搞$$代^@码网= find(in, fromVal, 0, r + 1, end – 1);
                int toCopy = r_1 – r + 1;
                System.arraycopy(in, r, out, i, toCopy);
                i += toCopy;
                out[i++] = fromVal;
                start++;
                r = r_1 + 1;
            }
        } while ((end – r) > 0 && (med – start) > 0);

        // copy rest of array
        if ((end – r) <= 0) {
            System.arraycopy(in, start, out, i, med – start);
        } else {
            System.arraycopy(in, r, out, i, end – r);
        }
    }

以上就是Java 位图法排序的使用方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java 位图法排序的使用方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址