Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the worst case time complexity of Java 14+ Arrays.sort( int[] )?

While I know this seems obvious I’ll explain my confusion. I’ve always thought of Quicksort’s worst case time complexity as O(n^2). The documentation for Arrays.sort(int[]) from Java 7 to Java 13 says:
This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.

The keyword here is "many", so I assume O(n log(n)) here refers to the average case, and there still exists data sets that result in a worst case of O(n^2).

But in Java 14 and up, the documentation for Arrays.sort(int[]) says:
This algorithm offers O(n log(n)) performance on all data sets.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So is the worst case for this improved implementation of Quicksort now O(n log(n)) ? Someone please clarify.

>Solution :

You need to check deeper implementation of Arrays.sort(int[]).

Inside there is a function DualPivotQuicksort.sort(…) which is used for sorting.

You can see in java 7 to 13 that function have a fail safe mechanism which changes sorting type if complexity is near O(n^2), it’s changing to QuickSort which average complexity is O(n log n) but it’s worst is O(n^2) that’s why in javaDoc description says "many data sets".

O the other hand since java 14 fail safe for higher complexity is changing sort algorithm to heapsort which have worst complexity of O(n log n) so it never will be slower than that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading