Java – why use two different algorithms for sorting?

In the arrays class, quick sort is used for sorting primitives, but for sorting objects, it is merge sort

I wonder why?

Solution

The reason for using mergeexport is that they want a stable algorithm - for example, where equal objects (by compareto() or compare()) are in the same relative order as before

For primitives, equality means "no difference" When {5,3,5} is classified as {3,5,5}, it used to be the first So we can use a faster (and unstable) quick sort algorithm here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>