1

I want to use the function

public static void sort(Object[] a)

To sort an int array but I am not sure how to do it so that I know definitely sure that it is using the merge sort and not any other sort.

Here is the Java documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

And below is what I think would be a correct implementation of the function in order to use the merge sort.

 public static void main(String[] args) {
     int arr[] = { 3, 2, 4, 1, 99, 30, 5, 3 };
     Arrays.sort(arr);
 }

Would this be correct? And further I wanted to specify an index from where to start sorting etc with

sort(Object[] a, int fromIndex, int toIndex)

How would I make sure to write up the code so that it used the merge sort and not quick sort.

3
  • 1
    The method you want to use expects an array of Objects. An int[] is an array of ints, not an array of Objects. Why do you care whether the algorithm is a merge sort or a quicksort? Commented Apr 21, 2019 at 9:01
  • @JBNizet I need to bench mark my own implementation of an algorithm with that of the java inbuilt ones Commented Apr 21, 2019 at 9:02
  • Then don't call Arrays.sort(int[]), since it uses a quicksort, and not a merge sort. Note that writing a correct benchmark in Java is a very complex task. If you don't know the difference between an int and an Object yet, you should probably concentrate on something other than writing benchmarks. Learn the basics first. Commented Apr 21, 2019 at 9:06

3 Answers 3

1

Arrays.sort method normally uses Quick Sort for arrays of primitives and Merge Sort for arrays of objects.

Therefore, in order to use Merge Sort, I guess you have to declare your array as an Integer object array, e.g.

// Unsorted array
Integer[] a = { 2, 6, 3, 5, 1 };
Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately, Arrays.sort uses both! It seems like, for your integer implementation it uses quicksort.

https://cafe.elharo.com/programming/java-programming/why-java-util-arrays-uses-two-sorting-algorithms/

2 Comments

How might I go about making it so that it only uses merge sort then? While still using arrays; and not collections etc.
Try using Integer object instead of int! You can also create your own class that implements Comparable, it should ensure merge sort from Arrays.sort
0

There is a legacy Merge Sort implementation in Arrays utility class encapsulated inside LegacyMergeSort class.

As mentioned by-default Arrays.sort will not use it. So you need to instruct the jvm to use it. This is as per the Java docs:

  Old merge sort implementation can be selected (for
  compatibility with broken comparators) using a system property.

The system property is java.util.Arrays.useLegacyMergeSort=true and you can set it either using java -Djava.util.Arrays.useLegacyMergeSort=true jar or using System.setProperty("java.util.Arrays.useLegacyMergeSort",true);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.