1

Suppose I want to use the mergesort algorithm. Does JAVA has a method/function which implements the mergesort algorithm?

Or do I have to "manually" code the algorithm?

I do not know JAVA at all.

6
  • Java (that's no acronym) is just a programming language. It doesn't have anything but the most primitive building blocks. There are, however, various Java libraries. Are you by chance asking for existing merge sort implementations written in Java?
    – user395760
    Oct 13, 2012 at 14:20
  • @delnan Java uses mergesort in its Sort method (Arrays and Collections classes). Oct 13, 2012 at 14:35
  • @LuiggiMendoza My pedantic point is that JDK != Java language. Also, AFAIK that's timsort, which, while heavily based on mergesort, is quite different. (At least different enough to warrant a name, a wikipedia page, and its own set of explanatory blog posts.)
    – user395760
    Oct 13, 2012 at 14:39
  • @delnan check the Collection#sort JavaDoc, posted in Reimeus's answer: Implementation note: This implementation is a stable, adaptive, iterative mergesort. So, every JDK must use mergesort to implement it. Oct 13, 2012 at 14:40
  • @LuiggiMendoza I have seen it (and now that I followed it, I recalled that they started using timsort only with version 7). Also note that I never claimed there aren't merge sort implementations in the JDK. I don't see how this note would require any specific implementation, as long as it is a stable sort. It's not like any Java program could ever determine the difference, right?
    – user395760
    Oct 13, 2012 at 14:41

3 Answers 3

5

Yes there is Arrays.sort (Object[] ), there is a source code sample from OpenJDK

1
  • So, need to manually code anything. From the moment I call arrays.sort in Java, under the hood, mergesort(modified) is working to achieve results Oct 13, 2012 at 18:04
1

You could use Collections.sort()

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). This algorithm offers guaranteed n log(n) performance.

-2

No there isn't a merge sort out of the box. A simple google after 'java mergesort' should give you plenty of useful results

2
  • 1
    You must read the JavaDoc or investigate before post when you don't know the answer. Oct 13, 2012 at 14:33
  • And the top google result is wikipedia which mentions that Java Array.sort implements merge sort.
    – weston
    Oct 13, 2012 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.