问题
Can anyone explain me this sentence please?
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist).
Link: Arrays.sort(Object[] arr)
I know how Merge works, but I still don't understand well. Thank you.
回答1:
Mergesort recursively merges sorted sublists. If the current sublists eligible for merging contain no overlapping elements, there's no need to merge them. The merge operation would be skipped.
Example:
List A
1 4 8 9
List B
10 12 14 19
There's no need to go through the process of comparing these lists because 9 is the largest element of A and 10 (the first element of B) is larger than the largest element of A. The result would just be the concatenation of A and B.
All the document is saying is that they take a shortcut if comprehensive processing is unnecessary.
回答2:
Let's say you are doing a merge sort and are about to merge your two sorted sublists, [1, 3, 4]
and [2, 5, 6]
. Then you must run your merge algorithm to interleave the two halves into the whole [1, 2, 3, 4, 5, 6]
.
[1] + [2] + [3, 4] + [5, 6] = [1, 2, 3, 4, 5, 6]
But let's say after you've sorted the two halves you have [1, 2, 3]
and [4, 5, 6]
. The highest element in the low sublist (3) is less than the lowest element in the high sublist (4). There's no need to merge the two sublists; you can simply concatenate them together.
[1, 2, 3] + [4, 5, 6] = [1, 2, 3, 4, 5, 6]
来源:https://stackoverflow.com/questions/4249794/java-sorting-algorithm