简体   繁体   English

为什么在Java 1.8中进行此类排序

[英]Why does this sort in Java 1.8

Taken from Java Puzzlers by Joshua Bloch and Neal Gafter 取自Joshua Bloch和Neal Gafter的Java Puzzlers

import java.util.*;

public class BananaBread {
    public static void main(String[] args) {
        Integer[] array = { 3, 1, 4, 1, 5, 9 };
        Arrays.sort(array, new Comparator<Integer>() {
            public int compare(Integer i1, Integer i2) {
                return i1 < i2 ? -1 : (i2 > i1 ? 1 : 0);
            }
        });
        System.out.println(Arrays.toString(array));
    }
}

The expected behaviour is undefined and the text says that it returns [3, 1, 4, 1, 5, 9]. 预期的行为是未定义的,文本说它返回[3,1,4,1,5,9]。 This is true up to Java Version 1.7. 这适用于Java V1.7。 However, in Java v. 1.8, the output is the sorted list. 但是,在Java v.1.8中,输出是排序列表。

I can see that Timsort is new in Java 1.8, but I'm not sure how the algorithm can function with an inconsistent comparator such as the one given above. 我可以看到Timsort是Java 1.8中的新功能,但我不确定算法如何使用不一致的比较器(例如上面给出的)来运行。 Any help or insight into how this can be would be greatly appreciated. 任何有关如何做到这一点的帮助或见解将不胜感激。

Java 8 uses a modied merge sort. Java 8使用修改的合并排序。 The key line it uses is 它使用的关键是

// From TimSort.binarySort
while (left < right) {
    int mid = (left + right) >>> 1;
    if (c.compare(pivot, a[mid]) < 0)  // compares for less than 0.
        right = mid;
    else
        left = mid + 1;
}

Note: it only cares about whether you return -1 or 0 (more specifically is < 0, true or false) 注意:它只关心你是返回-1还是0(更具体地说是<0,true或false)

Your comparator is the same as 你的比较器与

return i1 < i2 ? -1 : 0;

so in all the ways that matter for this code it's correct. 所以在这个代码的所有重要方面都是正确的。

Note: if you change the code like this 注意:如果您更改此代码

return i1 > i2 ? +1 : 0;

It doesn't sort anything. 它没有任何排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Java中的PriorityBlockingQueue无法正确排序? - Why does my PriorityBlockingQueue in Java not sort properly? Java为什么默认的Java版本是1.7,而不是1.8 - Java why default java version is 1.7, and not 1.8 为什么 Java 排序优于原语的计数排序 - Why does Java sort outperform Counting sort for primitives 为什么Java InetAddress.isReachable()不能像JRE 1.7.x那样在JRE 1.8.x上运行 - Why does Java InetAddress.isReachable() not work on JRE 1.8.x like on JRE 1.7.x 项目facet java的1.8版本不存在 - Version 1.8 of project facet java does not exist 为什么 -classpath 选项在 java 1.8 上不起作用 - Why -classpath option not working on java 1.8 为什么 ESAPI 编码器不适用于 Java 1.8 - Why ESAPI Encoder Not Working With Java 1.8 为什么Collections.sort(List)在Java 8中使用CopyOnWriteArrayList但在Java 7中不起作用? - Why does Collections.sort(List) work in Java 8 with CopyOnWriteArrayList but not in Java 7? Java不支持的主要次要版本52? 尽管我没有使用Oracle Java 1.8的新功能,为什么会发生这种情况? - Java Unsupported major minor Version 52? Why does this happen though I didn't use new features from Oracle Java 1.8? Java Casting:Java 11抛出LambdaConversionException而1.8抛出LambdaConversionException - Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM