2

I am new to java i have to sort an array of order 10^6. What is time complexity of sort method which is available in java. Which sorting algorithm should use?

8
  • Depends on your JVM: docs.oracle.com/javase/7/docs/api/java/util/Arrays.html "... does not have to be MergeSort, but has to be stable" Commented Aug 18, 2014 at 16:27
  • 2
    Arrays.sort() is a dual-pivot quicksort Commented Aug 18, 2014 at 16:27
  • How much RAM do you have at your disposal? Commented Aug 18, 2014 at 16:27
  • Clarify your question. Are you asking about the built in Java method? If yes - check the docs as @Marc B said. If no - This has nothing to do with Java, but with sorting algorithms, please take a look at sorting_algorithm to start off Commented Aug 18, 2014 at 16:30
  • which method does arrays.sort() used Commented Aug 18, 2014 at 16:30

3 Answers 3

4

Assuming you have no other knowledge of the data and have to use a generic sort method, the best theoretical in-place sort algorithm is O(n*log(n)). The Arrays.sort method should be using one of those, and is your best choice without more info.

If you're willing to use a lot of memory, you can use a non-in place sort such as radix or counting. These can be quicker than n*log(n), some as quick as O(n), but may use O(n) or worse memory. If you have knowledge of the data having special properties (such as it being almost already sorted) an insertion sort or similar algorithms could be quicker than O(n*log(n)) without using memory, but without more info one of those can't be suggested.

Sign up to request clarification or add additional context in comments.

1 Comment

RadixSort is not about having extra space, its about being able to refine partitions of the elements until you reach equivalence classes.
0

There are many different sorting algorithms and each with their own up and downsides. As far as I know there is no "sort algorithm collection" in Java that you could use so it's probably the best to implement the algorithm yourself.

Here is a list of sorting algorighms with a table of their properties. Which one is the best for you depends on the resources given and the requirements of the application.

Comments

0

Depends on the data to be sorted you can choose sorting algorithm , but if you don't know type of the data you can simply use Arrays.sort() O(n lg(n)) .

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.