Arrays.parallelSort vs Collections.sort [英] Arrays.parallelSort vs Collections.sort
问题描述
我正在检查想知道如何的帖子使用比较器
和橙色水果一直是第一个。从post toString方法丢失所以我添加到我的代码
I was checking a post that wanted to know how to use Comparator
and a Orange fruit to be first all the time. From the post toString method was missing so I added to my code
@Override
public String toString(){
return fruitName +" " + fruitDesc;
}
该帖子的回答是
使用Collection.sort
Collections.sort(fruits, new Comparator<Fruit>() {
@Override
public int compare(Fruit o1, Fruit o2) {
if (o1.getFruitName() != null && o1.getFruitName().equalsIgnoreCase("orange")){
return -1;
}
if (o2.getFruitName() != null && o2.getFruitName().equalsIgnoreCase("orange")){
return 1;
}
return o1.getFruitName().compareTo(o2.getFruitName());
}
});
输出:
Orange Orange description
Apple Apple description
Banana Banana description
Pineapple Pineapple description
我在想为什么不 Arrays.parallelSort
我被告知好的东西
I was thinking why not Arrays.parallelSort
which I have been told good stuff about
使用 Arrays.parallelSort
代码
using Arrays.parallelSort
code
Fruit[] arrayFruits = fruits.stream().toArray(Fruit[]::new);
Arrays.parallelSort(arrayFruits, (Fruit o1, Fruit o2) -> {
if (o1.getFruitName() != null && o1.getFruitName().equalsIgnoreCase("orange")){
return -1;
}
if (o2.getFruitName() != null && o2.getFruitName().equalsIgnoreCase("orange")){
return 1;
}
return o1.getFruitName().compareTo(o2.getFruitName());
});
输出:
Pineapple Pineapple description
Apple Apple description
Orange Orange description
Banana Banana description
对我来说排序是排序,为什么不同的答案形成不同的方法?
推荐答案
如果在 TryJava8 ,我得到了正确排序的数组。我想你可能打印输入( fruits
)而不是输出( arrayFruits
)。这就是说,你开了一个有趣的话题,因为一般来说,你是对的,排序算法不能保证完整的顺序。通常对于大型数组,如果两个元素是等效的,但不相同(例如,指向等效记录的不同指针),则算法不保证特定的顺序。这种说法通常用不同的算法区分不同。
If one runs the program in TryJava8, I get the correctly sorted array. I think you probably printed the input (fruits
) instead of the output (arrayFruits
). This said, you opened an interesting topic, since in general, you are right a sorting algorithm does not guarantee the full order. In general for large arrays, if two elements are equivalent, but not the same (for instance a different pointer to an equivalent record), the algorithms do not guarantee a specific order. This said ties are in general broken differently by different algorithms.
比较方法应该满足顺序关系约束:
订单关系应该是:
- 反身:每个项目都应该等于自己(你最好回报
0
我想) - 不对称:如果 A 小于或等于 B 和 B 小于或等于 A , A 且 B 相等。
- transitive:如果 A 小于或等于 B 且 B 小于或等于 C , A 小于或等于 C 。
- reflexive: every item should be equal to itself (you better return
0
I guess) - asymmetrical: if A is less than or equal to B and B is less than or equal to A, A and B are equal.
- transitive: if A is less than or equal to B and B is less than or equal to C, A is less than or equal to C.
大多数排序算法隐式地假设这些约束(它们不检查它们),因此提供 O(n log n)时间复杂度。如果条件不成立,取决于算法的实现,可以得到不同的结果。
Most of the sorting algorithms assume this constraints implicitly (they don't check them) and thus offer a O(n log n) time complexity. If the condition does not hold, depending on the implementation of the algorithm, one obtains different results.
由于并行排序使用 MergeSort
算法,默认排序使用 QuickSort
算法,这两种算法有不同的行为。
Since a parallel sort uses the MergeSort
algorithm, and a default sort uses the QuickSort
algorithm, the two algorithms have different behavior.
相关主题:大多数排序算法不稳定。假设两个项目相等,那么无法保证如果 A 放在原始数组中的 A'之前, A 将是放在结果数组中的 A'之前。
A relevant topic: most sorting algorithms are not stable. Say two items are "equal", then it is not guaranteed that if A was placed before A' in the original array, A will be placed before A' in the resulting array.
这篇关于Arrays.parallelSort vs Collections.sort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!