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?
3 Answers
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.
1 Comment
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.
Arrays.sort()is a dual-pivot quicksort